列表list
列表可以修改
列表:逗号分隔的不同数据项,使用方括号[]包围起来的就是一个列表,索引从0开始
- 访问列表中的值
可以用[]索引
可以用[:]切片
可以用for循环(enumerate()函数可以获取索引值)
访问列表中的值
2.修改和删除列表
可以用[]索引
可以用[:]切片
可以用for循环(enumerate()函数可以获取索引值)
修改或删除列表中的值
3.列表基本操作符
表达式 | 结果 | 描述 |
len([1,2,3,4]) | 4 | 列表长度 |
[1,2]+[3,4] | [1,2,3,4] | 组合 |
[1]*3 | [1,1,1] | 重复 |
1 in [1,2,3] | True | 判断元素是否在列表中 |
for i in [1,2,3]: print(i) |
1 2 3 |
迭代 |
4.列表的截取和拼接
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
L=['Google', 'Runoob', 'Taobao'] print(L[2]) print(L[-2]) print(L[1:])
结果:
'Taobao'
'Runoob'
['Runoob', 'Taobao']
拼接如3中的组合
5.嵌套列表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
A=['a',1] B=['b',2] C=[A,B] print(C) print(C[1]) print(C[0][1])
结果:
[['a',1],['b',2]]
['b',2]
1
6.列表函数
序号 | 方法(格式) | 描述 | 实例 |
1 |
max(list) and min(list)
|
返回列表元素中的最大(小)值。 |
![]() list1, list2 = ['Google', 'Runoob', 'Taobao'], [456, 700, 200] print ("list1 最大元素值 : ", max(list1)) print ("list2 最大元素值 : ", max(list2)) 结果 ![]() list1 最大元素值 : Taobao list2 最大元素值 : 700 ist1 最小元素值 : Google list2 最小元素值 : 200 |
2 |
list( seq )
|
list() 方法用于将元组转换为列表。 注:元组与列表是非常类似的,区别在于元组的元素值不能修改,元组是放在括号中,列表是放于方括号中。 |
![]() aTuple = (123, 'Google', 'Runoob', 'Taobao') list1 = list(aTuple) print ("列表元素 : ", list1) str="Hello World" list2=list(str) print ("列表元素 : ", list2) 结果 ![]() 列表元素 : [123, 'Google', 'Runoob', 'Taobao'] 列表元素 : ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
|
3 |
list.append(obj)
|
用于在列表末尾添加新的对象 |
![]() 1 list1 = ['Google', 'Runoob', 'Taobao'] 2 list1.append('Baidu') 3 print ("更新后的列表 : ", list1) 结果: ![]() 更新后的列表 : ['Google', 'Runoob', 'Taobao', 'Baidu']
|
4 |
list.count(obj)
|
用于统计某个元素在列表中出现的次数 |
![]() aList = [123, 'Google', 'Runoob', 'Taobao', 123]; print ("123 元素个数 : ", aList.count(123)) print ("Runoob 元素个数 : ", aList.count('Runoob')) 结果: ![]() 123 元素个数 : 2
Runoob 元素个数 : 1
|
5 |
list.extend(seq)
|
用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。 |
![]() list1 = ['Google', 'Runoob', 'Taobao'] list2=list(range(5)) # 创建 0-4 的列表 list1.extend(list2) # 扩展列表 print ("扩展后的列表:", list1) 结果: ![]() 扩展后的列表: ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]
|
6 |
list.index(obj)
|
用于从列表中找出某个值第一个匹配项的索引位置。 |
![]() list1 = ['Google', 'Runoob', 'Taobao'] print ('Runoob 索引值为', list1.index('Runoob')) print ('Taobao 索引值为', list1.index('Taobao')) 结果: ![]() Runoob 索引值为 1
Taobao 索引值为 2
|
7 |
list.insert(index, obj)
|
用于将指定对象插入列表的指定位置。 |
![]() list1 = ['Google', 'Runoob', 'Taobao'] list1.insert(1, 'Baidu') print ('列表插入元素后为 : ', list1) 结果: ![]() 列表插入元素后为 : ['Google', 'Baidu', 'Runoob', 'Taobao']
|
8 |
list.pop(obj=list[-1])
|
用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。 |
![]() list1 = ['Google', 'Runoob', 'Taobao'] list1.pop() print ("列表现在为 : ", list1) list1.pop(1) print ("列表现在为 : ", list1) 结果: ![]() 列表现在为 : ['Google', 'Runoob'] 列表现在为 : ['Google']
|
9 |
list.remove(obj)
|
用于移除列表中某个值的第一个匹配项 |
![]() list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.remove('Taobao') print ("列表现在为 : ", list1) list1.remove('Baidu') print ("列表现在为 : ", list1) 结果: ![]() 列表现在为 : ['Google', 'Runoob', 'Baidu'] 列表现在为 : ['Google', 'Runoob']
|
10 |
list.reverse()
|
用于反向列表中元素。 |
![]() list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.reverse() print ("列表反转后: ", list1) 结果: ![]() 列表反转后: ['Baidu', 'Taobao', 'Runoob', 'Google']
|
11 |
list.sort([func])
|
用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。 |
![]() list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.sort() print ("列表排序后 : ", list1) 结果: ![]() 列表排序后 : ['Baidu', 'Google', 'Runoob', 'Taobao']
|
12 |
list.clear()
|
用于清空列表,类似于 del a[:]。 |
![]() list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.clear() print ("列表清空后 : ", list1) 结果: ![]() 列表清空后 : []
|