序列是python最基本的数据结构,序列中的每个元素都分配一个位置数字,从0开始;可以进行的操作包括,切片,索引,加,检查成员。
列表的数据项不需要具有相同的类型;使用方括号逗号分隔来创建序列。
列表的元素可以修改,字符串和元组不可以。
访问序列的值
>>> list1=['hello',1,2.0]
>>> print(list1[1:])
[1, 2.0]
更新列表
>>> list1=['hello',1,2.0]
>>> list1.append('nosqldba')
>>> print(list1[1:])
[1, 2.0, 'nosqldba']
>>> list1[1]='world'
>>> print(list1[1:])
['world', 2.0, 'nosqldba']
删除列表元素
>>> list1=['hello',1,2.0]
>>> print(list1)
['hello', 1, 2.0]
>>> del list1[1]
>>> print(list1)
['hello', 2.0]
列表脚本操作符
Python 表达式 | 描述 |
len(list) | 长度 |
list1 + list2 | 组合 |
list * 4 | 重复 |
x in list | 元素是否存在于列表中 |
for x in list: print x, | 迭代 |
>>> list1=['hello',1,2.0]
>>> len(list1)
3
>>> list1+list1
['hello', 1, 2.0, 'hello', 1, 2.0]
>>> list1*2
['hello', 1, 2.0, 'hello', 1, 2.0]
>>> print(1 in list1)
True
>>> for x in list1:
... print(x)
...
hello
1
2.0
列表截取与拼接
+和*操作符和字符串类似
>>> list1=['hello',1,2.0]
>>> list1[1]
1
>>> list1[-1]
2.0
>>> list1[1:]
[1, 2.0]
>>> list1+list1
['hello', 1, 2.0, 'hello', 1, 2.0]
嵌套列表
>>> x=[1,2,3]
>>> y=[a,b,c]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> y=['a','b','c']
>>> z=[x,y]
>>> print(z)
[[1, 2, 3], ['a', 'b', 'c']]
>>> z[0][0]
1
列表函数和方法
函数
序号 | 函数 |
len(list) | 列表元素个数 |
max(list) | 返回列表元素最大值 |
min(list) | 返回列表元素最小值 |
list(seq) | 将元组转换为列表 |
>>> tuple1=(1,'china','usa')
>>> type(tuple1)
<class 'tuple'>
>>> list1=list(tuple1)
>>> type(list1)
<class 'list'>
方法
序号 | 方法 |
list.append(obj) | 在列表末尾添加新的对象 |
list.count(obj) | 统计某个元素在列表中出现的次数 |
list.extend(seq) | 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) |
list.index(obj) | 从列表中找出某个值第一个匹配项的索引位置 |
list.insert(index, obj) | 将对象插入列表 |
list.pop(obj=list[-1]) | 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 |
list.remove(obj) | 移除列表中某个值的第一个匹配项 |
list.reverse() | 反向列表中元素 |
list.sort([func]) | 对原列表进行排序 |
list.clear() | 清空列表 |
list.copy() | 复制列表 |
>>> list1=[3,2,1]
>>> list1.sort()
>>> print(list1)
[1, 2, 3]
>>> list2=list1
>>> print(list2)
[1, 2, 3]
>>> list3=list1.copy()
>>> print(list3)
[1, 2, 3]
>>> list1.remove(1)
>>> print(list1)
[2, 3]
列表当作堆栈使用
堆栈做为特定的数据结构,最先进入的元素最后一个被释放-后进先出;使用append()方法可以把一个元素添加到堆栈顶,用不指定索引的pop()方法可以把一个元素从堆栈顶部释放出来
>>> stack1 = [1,2,3,4]
>>> stack1.append(5)
>>> stack1.append(6)
>>> stack1
[1, 2, 3, 4, 5, 6]
>>> stack1.pop()
6
>>> stack1
[1, 2, 3, 4, 5]
列表当作队列使用
队列在第一个加入的元素第一个取出来,先进先出模式;不过用列表当队列效率不高,因为所有其他的的元素都需要一个一个移动
>>> from collections import deque
>>> queue1 = deque([1,2,3])
>>> queue1.append(4)
>>> queue1.append(5)
>>> queue1
deque([1, 2, 3, 4, 5])
>>> queue1.popleft()
1
>>> queue1
deque([2, 3, 4, 5])
列表推导式
>>> list1 = [2,4,6]
>>> [3*x for x in list1]
[6, 12, 18]
>>>
>>> [[x,x**2] for x in list1]
[[2, 4], [4, 16], [6, 36]]
>>>
>>> [3*x for x in list1 if x>3]
[12, 18]
>>>
>>> list2 = [' abc',' def ',' hij ']
>>> [x.strip() for x in list2]
['abc', 'def', 'hij']
>>>
>>> list3 = [1,2,3]
>>> list4=[4,5,6]
>>> [x*y for x in list3 for y in list4]
[4, 5, 6, 8, 10, 12, 12, 15, 18]
>>> [list3[i]*list4[i] for i in range(len(list3))]
[4, 10, 18]
嵌套列表解析
列表可以嵌套
>>> list5 = [
... [1,2,3,4],
... [5,6,7,8],
... [9,10,11,12]
... ]
>>> [[row[i] for row in list5] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
>>>
>>>
>>> list6=[]
>>> for i in range(4):
... list6.append([row[i] for row in list5])
...
>>> list6
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
>>>
>>> list7=[]
>>> for i in range(4):
... list7_row=[]
... for row in list5:
... list7_row.append(row[i])
... list7.append(list7_row)
...
>>> list7
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]