一、list列表,可迭代的对象
LI=[11,22,33,22,44,"age",["文学",[1,2],99,True]]
list类,通过list类创建的对象LI,中括号表示,逗号分隔每一个元素,列表中的元素可以是数字、字符串、列表、布尔值等,所有类型的数据都可以是列表的元素
列表是有序的,列表元素可修改
1.1 以下标或者索引的方式获取列表中的元素,返回结果是元素
LI=[1,33,"age",[44,"娜娜","abc"],False] print(LI[2]) 结果: age
1.2 以切片的方式获取列表中的元素,返回结果是列表
LI=[1,33,"age",[44,"娜娜","abc"],False] print(LI[0:3]) 结果: [1, 33, 'age']
1.3 支持forwhile循环,in操作
LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],False] s="" for r in LI: s = s +str(r) print(s) 结果: 133age[44, '娜娜', ['nn', 1233], 'abc']False
1.4 列表中的元素可以被修改、被删除
索引替换 LI=[1,33,"age",[44,"娜娜","abc"],False] LI[0]=120 print(LI) 结果: LI=[1,33,"age",[44,"娜娜","abc"],False] LI[0]=120 print(LI)
索引删除
LI=[1,33,"age",[44,"娜娜","abc"],False]
del LI [1]
print(LI)
结果:
[1, 'age', [44, '娜娜', 'abc'], False]
切片替换 LI=[1,33,"age",[44,"娜娜","abc"],False] LI[1:4]=["age",[11,"na"],666] print(LI) 结果:[1, 'age', [11, 'na'], 666, False]
切片删除
LI=[1,33,"age",[44,"娜娜","abc"],False]
del LI [1:3]
print(LI)
结果:
[1, [44, '娜娜', 'abc'], False]
ps:找到列表中的列表中的字符串:
LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],False]
v=LI[3][2][1]
print(v)
输出:1233
1.5 字符串可以转换为列表
方式一:列表中有数字和字符串时,需要使用for循环,str处理 LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],False] s="" for r in LI: s = s +str(r) print(s) 结果: 133age[44, '娜娜', ['nn', 1233], 'abc']False 方式二:列表中只包含数字时,join方法 LI=["123","abc","娜娜"] r="".join(LI) print(r) 结果: 123abc娜娜
2、list类中提供的对象调用方法介绍
例:list提供的方法,对象LI调用append的方法
2.1 append 原来的值最后边加元素
方式一: LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],False] LI.append(3) print(LI) 结果: [1, 33, 'age', [44, '娜娜', ['nn', 1233], 'abc'], False, 3] 方式二: LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],False] LI.append("abs") print(LI) 结果: [1, 33, 'age', [44, '娜娜', ['nn', 1233], 'abc'], False, 'abs'] 方式三: LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],False] LI.append([123,"娜娜",[123,123]]) print(LI) 结果: [1, 33, 'age', [44, '娜娜', ['nn', 1233], 'abc'], False, [123, '娜娜', [123, 123]]]
2.2 clear 清空列表
eg:LI.clear()
2.3 copy 浅拷贝
LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],False] v=LI.copy() print(v)
2.4 count指定元素出现的次数
LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],"age"] v=LI.count("age") print(v) 结果:2
2.5 extend 扩展源列表,传入可迭代对象
LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],"age"] LI.extend("age") print(LI) 结果: 把每个元素age循环放到列表中 [1, 33, 'age', [44, '娜娜', ['nn', 1233], 'abc'], 'age', 'a', 'g', 'e']
方式二: LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],"age"] LI.extend(["apple"]) print(LI) 结果: [1, 33, 'age', [44, '娜娜', ['nn', 1233], 'abc'], 'age', 'apple']
2.6 index 根据值获取当前值得索引位置(默认左边优先)
LI=[1,33,"age",[44,"娜娜",["nn",1233],"abc"],33] v=LI.index(33) print(v) 结果:1
2.7 insert 在指定索引位置插入元素
LI=[1,33,"age",33,[44,"娜娜",["nn",1233],"abc"],33] LI.insert(0,99) print(LI) 结果: [99, 1, 33, 'age', 33, [44, '娜娜', ['nn', 1233], 'abc'], 33] 在索引第一位插入99
2.8 pop 默认删除最后一个元素,并且可以获取删除的元素值
LI=[1,33,"age",33,[44,"娜娜",["nn",1233],"abc"],33] v=LI.pop() print(v) print(LI) 结果: 33 [1, 33, 'age', 33, [44, '娜娜', ['nn', 1233], 'abc']]
方式二:根据索引删除
LI=[1,33,"age",33,[44,"娜娜",["nn",1233],"abc"],33]
v=LI.pop(1)
print(v)
print(LI)
结果:
33
[1, 'age', 33, [44, '娜娜', ['nn', 1233], 'abc'], 33]
2.9 remove 只删除列表中的指定元素,左边优先
2.10 reverse 将当前列表进行反转
LI=[1,33,"age",33,[44,"娜娜",["nn",1233],"abc"],33] LI.reverse( ) print(LI) 结果: [33, [44, '娜娜', ['nn', 1233], 'abc'], 33, 'age', 33, 1]
2.11 sort 排序
LI=[1,33,66,33,44] LI.sort() print(LI) 结果: [1, 33, 33, 44, 66] 默认顺序排序 方式二: LI=[1,33,66,33,44] LI.sort(reverse=True) print(LI) 结果二: [66, 44, 33, 33, 1] 倒序