使用+来拼接列表
>>> a = [4,5,6]
>>> b = [1,2,3]
>>> a+b
[4, 5, 6, 1, 2, 3]
使用*来复制`
>>> a = [1,2,3]
>>> a = a*2
>>> a
[1, 2, 3, 1, 2, 3]
使用>,<,>=,等来进行字符串的比较(注意:列表中对应位置中元素的类型应该一致,否则会报错)
>>> a = [1,'b']
>>> b = [2,'a']
>>> a<b
True
>>> a == b
False
使用 in/not in来判断一个元素是否在列表中
>>> a = [1,2,3]
>>> a
[1, 2, 3]
>>> 1 in a
True
对于步长为一的切片替换,左右两边的总数可以不等,对于步长不为一的切片进行替换,左右两边的个数必须相等
>>> a = [1,2,3,4]
>>> a[1:3:] = [3,3,3,3]
>>> a
[1, 3, 3, 3, 3, 4]
使用copy()和切片复制都是浅拷贝
>>> L = [20,21,22]
>>> L1 = [1,L,3]
>>> L2 = L1
>>> L[1] = [3]
>>> L1
[1, [20, [3], 22], 3]
>>> L2
[1, [20, [3], 22], 3]
导入copy模块,使用deepcopy()来进行深拷贝函数复制
>>> import copy
>>> a = [1,2,3,4]
>>> b = copy.deepcopy(a)
>>> b[2] = 2
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 2, 4]
语法:[表达式 for 变量 in 可迭代对象]
>>> a = [1,2,3,4]
>>> b = [i**2 for i in a]
>>> b
[1, 4, 9, 16]
[表达式 for 变量 in 可迭代对象 if 条件判断]
>>> a
[1, 2, 3, 4]
>>> b = [i**2 for i in a if i%2==0]
>>> b
[4, 16]
方法一:
>>> a,b = 10,20
>>> a
10
>>> b
20
>>> a,b = b,a
>>> a
20
>>> b
10
方法二:
>>> a,b = 10,20
>>> a
10
>>> b
20
>>> a = a+b
>>> b = a-b
>>> a = a-b
>>> a
20
>>> b
10
方法三:
>>> a,b = 10,20
>>> a
10
>>> b
20
>>> a = a^b
>>> b = a^b
>>> a = a^b
>>> a
20
>>> b
10