python 列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 列表相关的知识,希望对你有一定的参考价值。
列表切片
name = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘]
查询:
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘]
>>> name[1]
‘b‘
>>> name[2:]
[‘c‘, ‘d‘, ‘e‘, ‘f‘]
>>> name[2:-1]
[‘c‘, ‘d‘, ‘e‘]
>>> name[-4:-2]
[‘c‘, ‘d‘]
>>> name[::-1]
[‘f‘, ‘e‘, ‘d‘, ‘c‘, ‘b‘, ‘a‘]
>>> name[::-2]
[‘f‘, ‘d‘, ‘b‘]
>>> name[1:5:2]
[‘b‘, ‘d‘]
>>> name[-1:-5:-1]
[‘f‘, ‘e‘, ‘d‘, ‘c‘]
>>> name[-1:0:-1]
[‘f‘, ‘e‘, ‘d‘, ‘c‘, ‘b‘]
>>> name[-2:1:-1]
[‘e‘, ‘d‘, ‘c‘]
>>> name[6:0:-1]
[‘f‘, ‘e‘, ‘d‘, ‘c‘, ‘b‘]
>>> name[6:2:-1]
[‘f‘, ‘e‘, ‘d‘]
查询格式为 list[起始位置:结束位置:步长]
list特点为顾头不顾腚,包含开头不包含结尾
步长默认为正序,步长1;可修改
如:2,3......
步长也可为负数,则查询结果为倒序
如:-1,-2,-3.....
正序时,起始和结束也为正序,起始在前结束在后
倒序时,起始和结束也需为倒序,起始在后结束在前
>>> name.append(‘g‘)
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
list.append(参数)括号中只能有一个参数
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
>>> name.insert(1,‘x‘)
>>> name
[‘a‘, ‘x‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
>>> name.insert(-2,‘y‘)
>>> name
[‘a‘, ‘x‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘y‘, ‘f‘, ‘g‘]
list.insert(位置,参数)
位置为下标也就是索引,
正序位置为下标位置,倒序位置为下标左侧位置
>>> name
[‘l‘, ‘a‘, ‘x‘, ‘b‘, ‘c‘, ‘z‘, ‘d‘, ‘e‘, ‘y‘, ‘f‘, ‘t‘, ‘g‘]
>>> name.remove(‘l‘)
>>> name
[‘a‘, ‘x‘, ‘b‘, ‘c‘, ‘z‘, ‘d‘, ‘e‘, ‘y‘, ‘f‘, ‘t‘, ‘g‘]
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘z‘, ‘d‘, ‘e‘, ‘y‘, ‘f‘, ‘t‘, ‘g‘]
>>> name.remove(name[3])
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘y‘, ‘f‘, ‘t‘, ‘g‘]
list.remove(参数) 一次只能有一个参数
参数可为列表中的值也可为值对应索引如
"z" = name[3]
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘y‘, ‘f‘, ‘t‘, ‘g‘]
>>> name.pop()
‘g‘
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘y‘, ‘f‘, ‘t‘]
>>> name.pop(6)
‘f‘
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘y‘, ‘t‘]
>>> name.pop(-2)
‘y‘
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘t‘]
list.pop(下标) 会将要删除的值输出,可引用
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘t‘]
>>> del name[-1]
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
>>> del name[3:]
>>> name
[‘a‘, ‘b‘, ‘c‘]
>>> name = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘]
>>> del name[::2]
>>> name
[‘b‘, ‘d‘, ‘f‘]
>>>
>>> del name
>>> name
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
name
NameError: name ‘name‘ is not defined
del list[起始位置:结束位置:步长]
del如只填1个参数则删除对应下标值,如存在起始和结束位置则删除该区间
del和切片类似,可根据步长进行删除,del可删除list
>>> name = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
>>> name1 = [‘A‘,‘b‘,‘C‘,‘D‘,‘E‘]
>>> name1
[‘A‘, ‘b‘, ‘C‘, ‘D‘, ‘E‘]
>>> name.extend(name1) #将name1扩展到name中
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘A‘, ‘b‘, ‘C‘, ‘D‘, ‘E‘]
>>> name1
[‘A‘, ‘b‘, ‘C‘, ‘D‘, ‘E‘]
>>> name.count(‘b‘) #name列表中‘b‘的个数
2
>>> name.count(‘a‘) #name列表中‘a‘的个数
1
>>> len(name) #name列表的长度,也就是元素个数
10
>>> len(name1)
5
>>> name.index(‘A‘) #name列表的中,‘A’的位置
5
>>> name.index(‘b‘) #name列表的中,第一个‘b’的位置
1
>>> name
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘A‘, ‘b‘, ‘C‘, ‘D‘, ‘E‘]
>>> name.reverse() #将name列表倒序
>>> name
[‘E‘, ‘D‘, ‘C‘, ‘b‘, ‘A‘, ‘e‘, ‘d‘, ‘c‘, ‘b‘, ‘a‘]
>>> name.sort() #将name列表按ASCII码从小到大的顺序排列
>>> name
[‘A‘, ‘C‘, ‘D‘, ‘E‘, ‘a‘, ‘b‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
>>> name.sort(reverse=-1) #将name列表按ASCII码从大到小的顺序排列
>>> name
[‘e‘, ‘d‘, ‘c‘, ‘b‘, ‘b‘, ‘a‘, ‘E‘, ‘D‘, ‘C‘, ‘A‘]
以上是关于python 列表的主要内容,如果未能解决你的问题,请参考以下文章