List 列表
1.引出list - 类似数组和结构体的综合 如何通过一个变量存储公司所有员工的名字? ![在这里插入图片描述](https://img-blog.csdnimg.cn/0b9cc228173a423c9d301b97bf62ef0e.png)
2.列表的功能
创建、查询、切片、增加、修改、删除、循环、排序
3.列表的定义和创建
定义:[ ] 内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素
复制代码
list创建1 (推荐)
>>> names = []
>>> names
[]
list创建2
>>> list()
[]
>>> names = list()
>>> names
[]
list创建3
>>> names = list(['alex',1])
>>> names
['alex', 1]
复制代码
4.列表的特点和常用操作
特性:
- 可存放多个值
- 按照从左到右的顺序定义列表元素,下标从0开始顺序访问,有序
- 可修改指定索引位置对应的值,可变
5.查询
>>> names = ['alex','jack','shanshan',4,4,5,6,7,7,8,9,4,4]
>>> names[0]
'alex'
>>> names[-1]
4
>>> names[-2]
4
>>> names[-3]
9
>>> names.index(6)
6
>>> names.index(4)
3
>>> i = names.index(6)
>>> names[i]
6
>>> names[names.index(6)]
6
列表的功能方法
>>> dir(names)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__'
, '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__'
, '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__'
, '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_e
x__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__s
izeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'ex
tend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
>>> names.count(4)
4
>>> names.count(6)
1
>>> names.count(7)
2
6.切片
![在这里插入图片描述](https://img-blog.csdnimg.cn/61c36ea37a7148dfa7b40e55903165f8.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/89ef235d7df14dd787d4847d5228c70a.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70)
>>> names
['alex', 'jack', 'shanshan', 4, 4, 5, 6, 7, 7, 8, 9, 4, 4]
>>>
>>> names[0-2]
4
>>> names[0-3]
9
>>> names[0:3]
['alex', 'jack', 'shanshan']
>>>
>>> names
['alex', 'jack', 'shanshan', 4, 4, 5, 6, 7, 7, 8, 9, 4, 4]
>>> names[3:0:-1]
[4, 'shanshan', 'jack']
![在这里插入图片描述](https://img-blog.csdnimg.cn/1174d7aada024d4681ad395bc07333c5.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/970fa978e1c84e968ba1a7d39d6380da.png)
7.增加和修改
追加 ![在这里插入图片描述](https://img-blog.csdnimg.cn/f72b51312685416e94072702de60ebe1.png) 插入 尽量少用,顺序存储,会导致大量移位,效率低下 ![在这里插入图片描述](https://img-blog.csdnimg.cn/109cad8c2d69434eb10bb2a43d665b29.png) 修改 ![在这里插入图片描述](https://img-blog.csdnimg.cn/3bcfa5637ed94090869892840f819473.png) 批量修改 ![在这里插入图片描述](https://img-blog.csdnimg.cn/f534e31b970c4dd49fb16ae9dc37f7ef.png)
8.删除
pop删除最后一个元素 ![在这里插入图片描述](https://img-blog.csdnimg.cn/72da138a5f254f2bb0070eaed3c5dc67.png) remove() 删除从左找到的第一个值 del 全局删除 ![在这里插入图片描述](https://img-blog.csdnimg.cn/5a61c745d173476a9c6924b69efe5e3b.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70)
9.循环
![在这里插入图片描述](https://img-blog.csdnimg.cn/fb88781e70234c8db40fb28c2b621bf9.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70)
>>> names
['alex', 5, 6, 7, 8, 9, 4, 4]
>>>
>>> for k in names:
... print('loop',k)
...
loop alex
loop 5
loop 6
loop 7
loop 8
loop 9
loop 4
loop 4
>>>
10 range
现在已经学了列表了,那现在同学们来一起创建一个从1-100的列表。
等1分钟,你们都打算怎么创建啊?
是直接从1写到100还是用循环?如果从1写到100未免太傻气了,用循环好像还好一点儿。但是用循环写还不够简单,在python里有一个现成的方法,可以直接生成一个1-100的数。
>>> range(1,100)
range(1, 100)
>>> list(range(1,100))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(0,100,2))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54,
56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
python2
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> for i in range(10):
... print(i)
...
0
1
2
3
4
5
6
7
8
9
>>>
>>> type(range(0,10))
<type 'list'>
python3
>>> range(0,10)
range(0, 10)
>>>
>>> for i in range(10):
... print(i)
...
0
1
2
3
4
5
6
7
8
9
>>> range(0,10)
range(0, 10)
>>>
>>> type(range(0,10))
<class 'range'>
11.排序:按ASCII码值排序
List是有序的 ![在这里插入图片描述](https://img-blog.csdnimg.cn/f4b3f9da9c34487bbb0f9eda8e9821db.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70) 反转 ![在这里插入图片描述](https://img-blog.csdnimg.cn/0363582aef2b40ad8fc864aede03f157.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/63cfda1eae6e4083b1db9ca9c2c0b7f5.png)
12.clear
n1.clear() n1 []
13.copy
![在这里插入图片描述](https://img-blog.csdnimg.cn/0507e377217f47f288e3992a9011cee5.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/b22408567f3a4f3eaf8c79cb4edfb9dc.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70) 浅拷贝原理 ---- 【重要】,浅拷贝是只拷贝一层(地址)。
![在这里插入图片描述](https://img-blog.csdnimg.cn/f433c2dcda0e4a41a1686821f6a5d904.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/a399bfd5a4c14661a9bf41ce5c42c2ee.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/dc8ec33ab3194b7c90cdb38b9f53aa2b.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xsdW9qaWFu,size_16,color_FFFFFF,t_70)
14. len,in,split和join
>>> len(l)
3
>>> 'seven' in l
True
>>> 'yuan' in l
False
>>> s = 'hello world'
>>> s.split(' ')
['hello', 'world']
>>> s2= 'hello,world'
>>> s2.split(',')
>>> l = ['hi','eva']
>>> '!'.join(l)
'hi!eva'
15.深copy,浅copy
浅copy
>>> n1
['a', 'b', 'c', ['alex', 'jack']]
>>> n2 = n1.copy()
>>> n2
['a', 'b', 'c', ['alex', 'jack']]
>>> id(n1)
12269064
>>> id(n2)
12269704
>>> n1[-1][-1] = 'tom'
>>> n1
['a', 'b', 'c', ['alex', 'tom']]
>>> n2
['a', 'b', 'c', ['alex', 'tom']]
>>> id(n1[-1])
12269512
>>> id(n2[-1])
12269512
或者
>>> import copy
>>> n1
['a', 'b', 'c', ['alex', 'tom']]
>>> n3 = copy.copy(n1)
>>> n3
['a', 'b', 'c', ['alex', 'tom']]
>>> n1[-1][-1] = '1111'
>>> n1
['a', 'b', 'c', ['alex', '1111']]
>>> n3
['a', 'b', 'c', ['alex', '1111']]
深copy ,比较浪费内存
>>> n1
['a', 'b', 'c', ['alex', '1111']]
>>> n4 = copy.deepcopy(n1)
>>> n4
['a', 'b', 'c', ['alex', '1111']]
>>> n1[-1][-1] = '222'
>>> n1
['a', 'b', 'c', ['alex', '222']]
>>> n4
['a', 'b', 'c', ['alex', '1111']]
>>> id(n1[-1])
12269512
>>> id(n4[-1])
12269960
|