L = [] #L绑定空列表
L = [1, 2, 3, 4]
L = ['beijing', 'shanghai', 'shenzhen']
L = [1, 'two', 3.3, '四']
L = [1, 2, [3.1, 3.2, 3.3], 4]
>>> list()
[]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> list(range(1,10,2))
[1, 3, 5, 7, 9]
x = [1, 2, 3]
y = [4, 5, 6, 7]
z = x + y #z = [1, 2, 3, 4, 5, 6, 7]
x = [1, 2, 3]
x += [4, 5, 6] #x = [1, 2, 3, 4, 5, 6]
x = [1, 2] * 3 #x=[1, 2, 1, 2, 1, 2]
y = 3 * [5, 6] #y=[5, 6, 5, 6, 5, 6]
x = [3, 4]
x *= 2 #x=[3, 4, 3, 4]
y = 3
y *= [7, 8] #y=[7, 8, 7, 8, 7, 8]
[1, 2, 3] < [1, 2, 4] #True
[1, 3] > [1, 2, 4] #True
[5] < [5, 0] #True
['ABC', '123'] > ['abc', '456'] #False
[1, 'two'] > ['two', 1] #报错
L = [1, 'Two', 3.3, '四']
1 in L # True
2 in L # False
3.3 in L # ???
'4' in L # True
列表是可变的序列,可以通过索引赋值改变列表中的元素
示例:
>>> L = [1, 2, 3, 4]
>>> L[2] = 3.3 #将第三个元素改为3.3
>>> L
[1, 2, 3.3, 4]
作用:
可以改变原列表的排序,可以插入和修改数据
可以用切片改变列表的对应元素的值
语法:
列表[切片] = 可迭代对象
注:赋值运算符的右侧必须是一个可迭代对象
示例:
>>> L = [2, 3, 4]
>>> L[0:1] = [1.1, 2.2]
>>> L
[1.1, 2.2, 3, 4]
>>> L = [2, 3, 4]
>>> L[1:] = [3.3, 4.4, 5.5]
>>> L
[2, 3.3, 4.4, 5.5]
>>> L = [2, 3, 4]
>>> L[:] = [0, 1]
>>> L
[0, 1]
>>> L = [2, 4]
>>> L[1:1] = [3]
>>> L
[2, 3, 4]
>>> L = [2, 4] # 实现在中间插入[3.1, 3.2]
>>> L[1:1] = [3.1, 3.2]
>>> L
>>> [2, 3.1, 3.2, 4]
>>> L = [2, 3, 4] # 实现在前面插入0, 1]
>>> L[0:0] = [0, 1]
>>> L
>>> [0, 1, 2, 3, 4]
>>> L = [2, 3, 4] # 实现在后面插入0, 1]
>>> L[3:3] = [5, 6]
>>> L
>>> [2, 3, 4, 5, 6]
>>> L = [1, 4] # 用range函数生成的可迭代对象赋值
>>> L[1:1] = range(2, 4) #
>>> L
>>> [1, 2, 3, 4]
>>> L = [2, 3, 4]
>>> L = [1:2]='ABCD' # 'ABCD'也是可迭代对象
>>> L
>>> [2, 3, 'ABCD']
L = [1, 2, 3, 4, 5, 6]
L[::2] = 'ABC' # 对的
# 以下切除三段,但给出5个元素填充是错的
L[::2] = 'ABCDE'
方法 | 意义 |
---|---|
L.index(v [, begin[, end]]) | 返回对应元素的索引下标, begin为开始索引,end为结束索引,当 value 不存在时触发ValueError错误 |
L.insert(index, obj) | 将某个元素插放到列表中指定的位置 |
L.count(x) | 返回列表中元素的个数 |
L.remove(x) | 从列表中删除第一次出现在列表中的值 |
L.copy() | 复制此列表(只复制一层,不会复制深层对象) |
L.append(x) | 向列表中追加单个元素 |
L.extend(lst) | 向列表追加另一个列表 |
L.clear() | 清空列表,等同于 L[:] = [] |
L.sort(reverse=False) | 将列表中的元素进行排序,默认顺序按值的小到大的顺序排列 |
L.reverse() | 列表的反转,用来改变原列表的先后顺序 |
L.pop([index]) | 删除索引对应的元素,如果不加索引,默认删除最后元素,同时返回删除元素的引用关系 |
L = [3.1, 3.2]
L1 = [1, 2, L] # [1, 2, [3.1, 3.2]]
L2 = L1.copy() # [1, 2, [3.1, 3.2]] 浅拷贝
L[0] = 3.14
L1 # [1, 2, [3.14, 3.2]]
L2 # [1, 2, [3.14, 3.2]] # L1和L2共同拥有L绑定的对象
import copy # 导入copy模块
L = [3.1, 3.2]
L1 = [1, 2, L] # [1, 2, [3.1, 3.2]]
L2 = copy.deepcopy(L1) # [1, 2, [3.1, 3.2]] 深拷贝
L[0] = 3.14
L1 # [1, 2, [3.14, 3.2]]
L2 # [1, 2, [3.1, 3.2]] 此列表不受影响
L1 = [1, 2, [3.1, 3.2]]
L2 = L1 # 会发生什么
L3 = L1.copy # 会发生什么
L4 = copy.deepcopy(L1) # 会发生什么
问题:
如何把L1里的3.1改为3.14
L1[2][0] = 3.14
示例split:
>>> s = 'Beijing is capital'
>>> l = s.split(' ')
>>> l
['Beijing', 'is','capital']
示例join:
>>> l = ['C:', 'Programe files', 'Python3']
>>> s = '\\'.join(l)
>>> s
'C:\\Programe files\\Python3'
'''
问题:
如何生成下列列表?
[1, 4, 9, 16, 25, ……, 81]
'''
l = []
for i in range(1, 10):
l.append(i ** 2)
# 用列表推导式生成:
l = [i ** 2 for i in range(1, 10)]
示例:
'''
生成[1,9,25,49,81]列表,跳过所有的偶数
'''
l = [x ** 2 for x in range(1, 10) if x % 2 == 1]
'''
想生成
['a1', 'a2', 'a3','b1', 'b2', 'b3','c1', 'c2', 'c3',]
'''
l = [x + y for x in 'abc' for y in '123']