列表
- 列表是排列整齐的队伍
- 列表的中内的个体称作元素
- 元素可以是任意对象(数字、字符转、对象、列表等)
- 列表是可变的
列表的定义,用 [ ] 表示
- list = list() --> new empty list
- list = (iterable) --> new list initialized from iterable's items
list_one = list() lst_two = [] list_thr = (list(range5))
-
列表可以无限扩展,不能一开始定义大小
- java ,c中 可以定义列表的大小
列表的方法内置方法
![](https://img-blog.csdnimg.cn/20210202211516914.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NtYXJ0X2xvb2s=,size_16,color_FFFFFF,t_70)
- 列表的索引
- 正索引:从左到右,从0开始
- 负索引:从右到左,从-1开始
- list [ index ] # index 为索引
list_one = [1,3,2,1,6,7,9] print(list_one[-2]) print(list_one[3]) 结果: 7 1
- list[ index ] = value #通过索引来改列表中数值
list_one = [1,3,2,1,6,7,9] list_one[3] = "小黑" print(list_one) 结果: [1, 3, 2, '小黑', 6, 7, 9]
- list.index(value,[start,[stop]]) #通过值value,从指定区间查找列表内的元素是否匹配;匹配第一个就立即返回索引; 匹配不到,抛出异常ValueError
list_one = [1,3,2,1,6,7,9] print(list_one.index(1,2)) 结果: 3
- count(value) # 返回列表中匹配value的次数
list_one = [1,3,2,1,6,7,9] print(list_one.count(1)) 结果: 2
- len(list) #统计列表个数
list_one = [1,3,2,1,6,7,9] print(len(list_one)) 结果: 7
- list.append(value) #在列表末尾增加单个元素;返回None就意味着没有新的列表产生,就地修改;建议使用,时间复杂度O(1)
list_one = [1,3,2,1,6,7,9] --> None list_one.append("小红") print(list_one) 结果: 1, 3, 2, 1, 6, 7, 9, '小红']
- list.insert(value,10) #在指定的索引index处插入元素单个元素;返回None就意味着没有新的列表产生,就地修改;建议不要使用,更改索引后,列表中的所有的元素的索引都要改变,时间复杂度O(n)
list_one = [1,3,2,1,6,7,9] list_one.insert(10,12) print(list_one) 结果: [1, 3, 2, 1, 6, 7, 9, 12]
列表的批量增加
- list1 + list 2 -> new list #连接操作,将两个列表连接起来产生新的列表,原列表不变;本质上调用的是__add__()方法
list_one = [1,3,2,1,6,7,9] list_two = ["a","b","c"] list_one + list_two -> list print(list_one+ list_two) 结果: [1, 3, 2, 1, 6, 7, 9, 'a', 'b', 'c']
- extend(iterable) -> None #将可迭代对象的元素追加进来,返回None就地修改;在末尾追加
list_one = [1,3,2,1,6,7,9] list_two = ["a","b","c"] list_one.extend(list_two) print(list_one) 结果: [1, 3, 2, 1, 6, 7, 9, 'a', 'b', 'c']
- * -> new list #将本列表元素重复n次,返回新的列表
list_two = ["a","b","c"] list_th = list_two*2 print(list_th) 结果: ['a', 'b', 'c', 'a', 'b', 'c']
列表删除操作
- list.pop(index) --> item #不指定索引默认从列表末尾删除,返回删除的元素
list_two = ["a","b","c"] print(list_two.pop()) #默认没有加索引 print(list_two) 结果: c ['a', 'b'] list_two = ["a","b","c"] print(list_two.pop(1)) #指定索引删除 print(list_two) 结果: b ['a', 'c']
- list.clear() -> None #清除列表所有元素,剩下一个空列表 ,大规模删除后,会引起垃圾回收的问题
list_two = ["a","b","c"] list_two.clear() print(list_two) 结果: []
- list.remove(value) -> None #从左至右查找第一个匹配value的值,移除该元素,返回None
list_two = ["a","b","b","c"] list_two.remove("b") print(list_two) 结果: ['a', 'b', 'c']
列表其他操作
- list.reverse() ->None #p 将列表元素反转,返回None
list_two = ["a","b","c"] list_two.reverse() print(list_two) 结果: ['c', 'b', 'a']
- list.sort(key=None,reverse=False) -> None #对列表元素进行排序,就地修改,默认升序;reverse为True,反转,降序;key一个函数,指定key如何排序list.sort(key=functionname)
list_two = [2,1,3,23,8,9] list_two.sort() print(list_two) 结果: [1, 2, 3, 8, 9, 23] list_two = [2,1,3,23,8,9] list_two.sort(reverse = True) print(list_two) 结果: [23, 9, 8, 3, 2, 1] list_two = [2,1,3,23,8,9,"b","m"] list_two.sort(key = str) print(list_two) 结果: [1, 2, 23, 3, 8, 9, 'b', 'm']
- in # 返回的是bool
list_two = [2,1,3,23,8,9,"b","m"] print(2 in list_two) 结果: True