标签:bfd 嵌套 des ebe src 统计 end 反转 code
list常用命令:
索引/切片:list[index] list[start_index:end_index]
增:list.append(obj) list.insert(index,obj) list.extend(obj)
删:list.pop(index) list.pop() list.remove(obj) list.clear() del list del list[start_inde:end_index]
改:list[index] = obj list[start_index:end_indes] = obj
查:for i in list: list[index] list[start_index:end_indes]
统计: len(list) list.count(obj) list.index(obj)
排序:list.sort() list.sort(reverse=True) list.reverse()
嵌套查询:list[index][index]
1,切片:同str顾头不顾尾
li[0],li[0:3]
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1[3] print(l2)
l1 = [1,2,3,[3,2,4],4] l2 = l1[0:3] print(l2)
2,增加:无返回值
追加:li.append(‘e‘)
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.append([7,8,9]) print(l1)
l1 = [1,2,3,[3,2,4],4] l2 = l1.append(‘eee‘) print(l1)
插入:li.insert(4,‘e‘)--在索引为4处插入e
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.insert(2,‘tt‘) print(l1)
分解追加元素:li.extend(‘[1,2,3]‘)--在li尾部追加1,2,3三个元素
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.extend([‘a‘,‘b‘]) print(l1)
3,删除:返回删除的值
li.pop(2)--2表示索引删除
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.pop(1) print(l1)
li.pop()--默认删除最后一个元素
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.pop() print(l1)
li.remove(‘e‘)--e表示按元素删除
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.remove(3) print(l1)
li.clear()--清除列表
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.clear() print(l1)
del li--完全删除列表
ey:
l1 = [1,2,3,[3,2,4],4] del l1 print(l1)
del li[0:2]--切片删除列表
ey:
l1 = [1,2,3,[3,2,4],4] del l1[0:2] print(l1)
4,更改值:
li[0] = ‘e‘ --按索引赋值更改值
ey:
l1 = [1,2,3,[3,2,4],4] l1[1] = ‘e‘ print(l1)
li[0:2] = ‘ssse‘--按切片赋值更改,元素分解增加值
ey:
l1 = [1,2,3,[3,2,4],4] l1[1:2] = ‘eaas‘ print(l1)
result:[1, ‘e‘, ‘a‘, ‘a‘, ‘s‘, 3, [3, 2, 4], 4]
li[0:2] = [1,2,‘eee‘] --列表插入
ey:
l1 = [1,2,3,[3,2,4],4] l1[0:2] = [‘a‘,2,‘b‘] print(l1)
result:[‘a‘, 2, ‘b‘, 3, [3, 2, 4], 4]
5,查询:
for 循环查找:for i in li:
ey:
l1 = [1,2,3,[3,2,4],4] for li in l1: print(li)
切片查找:li[0:2]
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1[0] print(l2)
result:1
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1[0:3] print(l2)
result:[1, 2, 3]
6,公共方法:
长度:len(li)
ey:
l1 = [1,2,3,[3,2,4],4] l2 = len(l1) print(l2)
result:5
计数:li.count(‘e‘)--返回e个数
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.count(3) print(l2)
result:1
索引:li.index(‘e‘)--返回e的索引,如果e不存在报错
ey:
l1 = [1,2,3,[3,2,4],4] l2 = l1.index(3) print(l2)
result:2
7,排序:
数字从小到大排序-正序:li.sort()
ey:
l1 = [1,2,3,5,4] l2 = l1.sort() print(l1)
result:[1, 2, 3, 4, 5]
数字倒序:li.sort(reverse=true)
ey:
l1 = [1,2,3,5,4] l2 = l1.sort(reverse=True) print(l1)
result:[5, 4, 3, 2, 1]
反转:li.reverse()
ey:
l1 = [1,2,-3,-5,4] l2 = l1.reverse() print(l1)
result:[4, -5, -3, 2, 1]
8,列表嵌套:
li[1][1]---找到列表li第2个元素为str,找到str第二个元素
ey:
l1 = [1,2,-3,[7,8,9,‘e‘],4] l2 = l1[3][3] print(l2)
result:e
标签:bfd 嵌套 des ebe src 统计 end 反转 code
原文地址:https://www.cnblogs.com/meifei/p/11384482.html