列表
创建列表
使用中括号,将所有准备放入列表中的元素包裹起来,不同元素之间使用逗号分隔。
列表可以存放不同类型的元素
创建+打印
x = [1,2,3,4,5,"上山打老虎"]
>>> print(x)
[1, 2, 3, 4, 5, '上山打老虎']
>>> for i in x:
print(i)
1
2
3
4
5
上山打老虎
>>>
访问列表中的单个元素
>>> x[0]
1
>>> x[1]
2
>>> x[5]
'上山打老虎'
>>> x[-1]
'上山打老虎'
列表切片
一次性获取多个元素
给定一个区间 如 x[0:3] 遵循左开右闭 取出相应下标下对应的元素
x[:3] 省略开始位置,即从下标0开始
x[3:] 省略结束位置,即直到取出所有元素
x[:] 只有一个冒号,取出所有元素
x[0:6:2] 前面两个数确定区间,最后一个数确定跨度
x[::2] 由于上一条区间是从头到尾,故可以省略
x[::-2] 倒过来输出,跨度为-2
x[::-1] 倒过来输出整个列表
>>> x[0:3]
[1, 2, 3]
>>> x[3:6]
[4, 5, '上山打老虎']
>>> x[:3]
[1, 2, 3]
>>> x[3:]
[4, 5, '上山打老虎']
>>> x[:]
[1, 2, 3, 4, 5, '上山打老虎']
>>> x[0:6:2]
[1, 3, 5]
>>> x[::2]
[1, 3, 5]
>>> x[::-2]
['上山打老虎', 4, 2]
>>> x[::-1]
['上山打老虎', 5, 4, 3, 2, 1]
列表的方法
增
append() 在列表的末尾添加一个指定的元素
>>> y = ["cjr","chh"]
>>> y.append("jwx")
>>> y
['cjr', 'chh', 'jwx']
extend() 方法的参数必须是一个可迭代对象,新的内容追加到原列表最后一个元素的后面
>>> y.extend(["lyx","zxx"])
>>> y
['cjr', 'chh', 'jwx', 'lyx', 'zxx']
使用切片的方法添加元素
>>> s = [1,2,3,4,5]
>>> s[len(s):] = [6]
>>> s
[1, 2, 3, 4, 5, 6]
>>> s[len(s):] = [7,8,9]
>>> s
[1, 2, 3, 4, 5, 6, 7, 8, 9]
insert() 第一个参数指定待插入的位置 第二个参数指定待插入的元素
>>> s = [1,3,4,5]
>>> s.insert(1,2)
>>> s
[1, 2, 3, 4, 5]
>>> s.insert(0,0)
>>> s
[0, 1, 2, 3, 4, 5]
>>> s.insert(6,6)
>>> s
[0, 1, 2, 3, 4, 5, 6]
删
remove() 方法 指定具体的元素数值,将该数值从列表中删除
1.如果列表中存在多个匹配的元素,那么它只会删除第一个
2.如果指定的元素不存在,那么程序就会报错
当需要删除某个位置上的元素时,使用pop() 方法,它的参数就是元素的下标索引值
>>> s.remove(3)
>>> s
[1, 2, 4, 5]
>>> s.pop(3)
5
>>> s
[1, 2, 4]
>>> s.clear()
>>> s
[]
改
>>> s = [1,2,3,4,5,6,7,8,9]
>>> s
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s[4] = 0
>>> s
[1, 2, 3, 4, 0, 6, 7, 8, 9]
>>> s[5:] = [10,11,12,13]
>>> s
[1, 2, 3, 4, 0, 10, 11, 12, 13]
排序
>>> nums = [3,7,5,4,9,8,2,1]
>>> nums.sort()
>>> nums
[1, 2, 3, 4, 5, 7, 8, 9]
>>> nums.reverse()
>>> nums
[9, 8, 7, 5, 4, 3, 2, 1]
>>> nums = [3,7,5,4,9,8,2,1]
>>> nums
[3, 7, 5, 4, 9, 8, 2, 1]
>>> nums.sort(reverse = True)
>>> nums
[9, 8, 7, 5, 4, 3, 2, 1]
查
count() 统计元素一共有几个
index(a) 获取指定元素的下标索引值,如果有多个相同的元素,则返回找到的第一个元素的下标
index(a,b,c) 三个参数 a是要查找的元素,查找的区间是[b,c]
>>> nums = [1,2,3,3,3,3,4,5,6,7]
>>> nums.count(3)
4
>>> nums.index(3)
2
>>> nums[nums.index(4)] = 8
>>> nums
[1, 2, 3, 3, 3, 3, 8, 5, 6, 7]
>>> nums.index(3)
2
>>> nums.index(3,3,9)
3
采用直接列表赋值的效果
b的元素受a元素的变化影响
>>> a = [1,2,3,4]
>>> b= a
>>> b
[1, 2, 3, 4]
>>> a[0]=3
>>> a
[3, 2, 3, 4]
>>> b
[3, 2, 3, 4]
采用copy() b的元素不会受到a元素的变化影响
>>> a = [1,2,3,4]
>>> b = a.copy()
>>> b
[1, 2, 3, 4]
>>> a[0]=3
>>> a
[3, 2, 3, 4]
>>> b
[1, 2, 3, 4]
也可以使用切片进行赋值
>>> a = [1,2,3,4]
>>> b = a[:]
>>> b
[1, 2, 3, 4]
>>> a[0]=3
>>> a
[3, 2, 3, 4]
>>> b
[1, 2, 3, 4]
加法和乘法
>>> s = [1,2,3]
>>> t = [4,5,6]
>>> s+t
[1, 2, 3, 4, 5, 6]
>>> s*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
嵌套列表
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> matrix = [[1,2,3],
[4,5,6],
[7,8,9]]
>>> matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
访问
>>> for i in matrix:
for each in i:
print(each,end=' ')
print()
1 2 3
4 5 6
7 8 9
>>> matrix[0]
[1, 2, 3]
>>> matrix[0][0]
1
通过循环语句创建嵌套列表
>>> A = [0]*3
>>> for i in range(3):
A[i] = [0]*3
>>> A
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
有关浅拷贝和深拷贝
>>> x = [[1,2,3],[4,5,6],[7,8,9]]
>>> y = x.copy()
>>> x[1][1]=1
>>> x
[[1, 2, 3], [4, 1, 6], [7, 8, 9]]
>>> y
[[1, 2, 3], [4, 1, 6], [7, 8, 9]]
>>> import copy
>>> x = [[1,2,3],[4,5,6],[7,8,9]]
>>> y = copy.copy(x)
>>> x[1][1] = 1
>>> x
[[1, 2, 3], [4, 1, 6], [7, 8, 9]]
>>> y
[[1, 2, 3], [4, 1, 6], [7, 8, 9]]
>>> x = [[1,2,3],[4,5,6],[7,8,9]]
>>> y = copy.deepcopy(x)
>>> x[1][1]=1
>>> x
[[1, 2, 3], [4, 1, 6], [7, 8, 9]]
>>> y
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
将每个元素都乘以2,
1.下标索引遍历(本质上还是原来的列表)
>>> x = [1,2,3,4,5]
>>> for i in range(len(x)):
x[i] = x[i] * 2
>>> x
[2, 4, 6, 8, 10]
2.使用列表推导式
>>> x = [1,2,3,4,5]
>>> x = [i*2 for i in x]
>>> x
[2, 4, 6, 8, 10]
列表推导式
列表推导式创建嵌套列表
>>> A = [[0]*3 for i in range(3)]
>>> A
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> A[1][1]=1
>>> A
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]
列表推导式还可以添加一个用于筛选的if分句
>>> even = [i for i in range(10) if i%2==0]
>>> even
[0, 2, 4, 6, 8]
嵌套的列表推导式
对应嵌套的循环
如,将二维列表降为一维列表
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> flatten = [col for row in matrix for col in row]
>>> flatten
[1, 2, 3, 4, 5, 6, 7, 8, 9]
嵌套的列表推导式,每个for语句后面都可以加一个if进行筛选
>>> [[x,y] for x in range(10) if x%2==0 for y in range(10) if y%3==0]
[[0, 0], [0, 3], [0, 6], [0, 9], [2, 0], [2, 3], [2, 6], [2, 9], [4, 0], [4, 3], [4, 6], [4, 9], [6, 0], [6, 3], [6, 6], [6, 9], [8, 0], [8, 3], [8, 6], [8, 9]]
],[4,5,6],[7,8,9]]
flatten = [col for row in matrix for col in row]
flatten
[1, 2, 3, 4, 5, 6, 7, 8, 9]
嵌套的列表推导式,每个for语句后面都可以加一个if进行筛选
```python
>>> [[x,y] for x in range(10) if x%2==0 for y in range(10) if y%3==0]
[[0, 0], [0, 3], [0, 6], [0, 9], [2, 0], [2, 3], [2, 6], [2, 9], [4, 0], [4, 3], [4, 6], [4, 9], [6, 0], [6, 3], [6, 6], [6, 9], [8, 0], [8, 3], [8, 6], [8, 9]]