python操作列表的方法

1 列表
1.1 分片和排序

def func():
	listA = [3,2,1,4,9,5,6]
	listB = listA[:]#分片
	listB = listB.sort()#排序
print listB
>>> [1, 2, 3, 4, 5, 6, 9]

1.2 添加元素

lst = [1,2,3]
lst.append(5)
print lst
>>> [1, 2, 3, 5]

1.3 计算列表元素出现的次数

lst = [1,3,5,2,3,5,1,2,4,1]
num = lst.count(1)
print num
>>> 3

1.4 扩展已有列表

a = [1,3,5,2,3,5,1,2,4,1]
b = [1,3,5,2]
a.extend(b)
print a
>>>  [1, 3, 5, 2, 3, 5, 1, 2, 4, 1, 1, 3, 5, 2]

1.5 通过索引确定元素在列表中的位置

c = ['test','vox','qpq','rew']
c.index('test')
print c.index('test')
print c[1]

你可能感兴趣的:(python)