列表list
Python的列表是一个有序集合,可以添加和删除元素。
比如定义一个list
name = ['zhangyang','guyun','xiaoming'] print(name)#['zhangyang', 'guyun', 'xiaoming']
如果我要取第一个元素,可以直接通过列表的key值取,列表的key是从0开始的,所以取key值等于0的元素就把列表第一个元素取出来了。
print(name[0])#zhangyang
list切片
Python中的有序序列都支持切片,切片的返回结果类型和切片对象类型一致,返回的是切片对象的子序列。如列表切片返回一个列表,字符串切片返回一个字符串。
切片的格式是:list [ start : end : step ] 。
start是开始索引,end是切片终点索引,但是不包含当前索引的值,step是步长默认为1。
正向切片
比如取key值为1-5的元素:
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] print(name[1:6])#['guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
python 同样支持反向索引
比如要取最后最后一个元素:
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] print(name[-1])#wamgwu print(name[-2])#lisi
反向切片
反向切片必须保证start 和end 方向一致 ,方向都是从左到右
比如我要取上面列表后3个值,那么开始位置应该是-3,结束位置留空,留空表示列表结尾,
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] print(name[-3:])#['zhangshan', 'lisi', 'wamgwu']
列表的添加
append()方法,追加元素到列表末尾。
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name.append('zhaoliu') print(name)#['zhangyang', 'guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu', 'zhaoliu']
insert(key,value) 插入元素到指定位置 key是位置,value是要插入的值
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name.insert(1,'tianqi') print(name)#['zhangyang', 'tianqi', 'guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
列表的修改
list[key] = value 指定key赋值一个新值。
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name[2] = 'xiaowang' print(name)#['zhangyang', 'guyun', 'xiaowang', 'zhangshan', 'lisi', 'wamgwu']
列表的删除
1 、remove(value) 指定要删除的值 。
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name.remove('guyun') print(name)#['zhangyang', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
2、del list[key] 删除指定的key del 方法不止可以删除列表 。
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] del name[1] print(name)#['zhangyang', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
3、pop(key) 删除指定的key
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name.pop(1) print(name)#['zhangyang', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
查找列表的key
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] print(name.index('xiaoming'))#2
列表的统计
count(value) 统计列表中有多少相同的key
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] print(name.count('xiaoming'))#1
清空列表
把列表清空。
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name.clear() print(name)#[]
列表反转
reverse() 反转列表里面的元素
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name.reverse() print(name)#['wamgwu', 'lisi', 'zhangshan', 'xiaoming', 'guyun', 'zhangyang']
列表的排序
sort() 安装ACSII码排序。
name = ['22zhangyang','!@guyun','xiaoming','zhangshan','lisi','wamgwu'] name.sort() print(name)#['!@guyun', '22zhangyang', 'lisi', 'wamgwu', 'xiaoming', 'zhangshan']
列表的合并
extend(list1,list2) 合并两个列表,把list2的内容合并到list1里面去,list2的内容还在。
name = ['22zhangyang','!@guyun','xiaoming','zhangshan','lisi','wamgwu'] name2 = [1,2,3,4,5] name.extend(name2) print(name,name2)#['22zhangyang', '!@guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu', 1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
如果我修改list2里面的内容 那么合并到list1里面的内容会跟着修改吗?
name = ['22zhangyang','!@guyun','xiaoming','zhangshan','lisi','wamgwu'] name2 = [1,2,3,4,5] name.extend(name2) name2[2] = 'xiaowang' print(name,name2)#['22zhangyang', '!@guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu', 1, 2, 3, 4, 5] [1, 2, 'xiaowang', 4, 5]
可以看到 当我把 list2 合并到 list1 列表后,修改list2 对 list1 并没有影响,说明list1是对list2做了数据复制,而不是调用引用。
多层列表
多层列表相当于php中的多维数组。
name2 = [1,2,3,4,5] name = ['zhangyang','guyun',name2 ,'xiaoming','zhangshan','lisi','wamgwu'] name3 = ['zhangyang','guyun',[1,2,3,4,5] ,'xiaoming','zhangshan','lisi','wamgwu'] print(name )#['zhangyang', 'guyun', [1, 2, 3, 4, 5], 'xiaoming', 'zhangshan', 'lisi', 'wamgwu'] print(name3 )#['zhangyang', 'guyun', [1, 2, 3, 4, 5], 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
多层列表的打印和单层列表的打印是一样的
name2 = [1,2,3,4,5] name = ['zhangyang','guyun',name2 ,'xiaoming','zhangshan','lisi','wamgwu'] print(name[2][0] )#1
列表长度
len(list) 统计列表长度。
name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] print(len(name) )#6
如果列表是空的那么长度为0 。
列表复制
浅copy
copy()
name2 = [1,2,3,4,5] name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name3 = name.copy() print(name )#['zhangyang', 'guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu'] print(name3 )#['zhangyang', 'guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
同样,如果我在列表被复制后修改源列表的值,那么新列表会跟着改吗?
name2 = [1,2,3,4,5] name = ['zhangyang','guyun','xiaoming','zhangshan','lisi','wamgwu'] name3 = name.copy() name[1] = 'xiaoyu' print(name )#['zhangyang', 'xiaoyu', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu'] print(name3 )#['zhangyang', 'guyun', 'xiaoming', 'zhangshan', 'lisi', 'wamgwu']
修改了之后,新列表和源列表并没什么影响。
如果是多层列表呢?在源列表里面嵌套一个小列表试下:
name = ['zhangyang','guyun','xiaoming',['alex','michael'],'zhangshan','lisi','wamgwu'] name3 = name.copy() name[1] = 'xiaoyu' name[3][0] = 'ALEX' print(name )#['zhangyang', 'xiaoyu', 'xiaoming', ['ALEX', 'michael'], 'zhangshan', 'lisi', 'wamgwu'] print(name3 )#['zhangyang', 'guyun', 'xiaoming', ['ALEX', 'michael'], 'zhangshan', 'lisi', 'wamgwu']
可以看到,嵌套的列表在复制后修改一样影响新列表。那么在源列表中这个嵌套的小列表只是一个地址的引用而不是真正的数据。
以上说明copy()方法只能复制最外层的列表,这种复制叫做浅copy。
深copy
这里要使用copy模块
import copy name = ['zhangyang','guyun','xiaoming',['alex','michael'],'zhangshan','lisi','wamgwu'] name3 = copy.deepcopy(name) name[1] = 'xiaoyu' name[3][0] = 'ALEX' print(name )#['zhangyang', 'xiaoyu', 'xiaoming', ['ALEX', 'michael'], 'zhangshan', 'lisi', 'wamgwu'] print(name3 )#['zhangyang', 'guyun', 'xiaoming', ['alex', 'michael'], 'zhangshan', 'lisi', 'wamgwu']
这里可以看到,嵌套的列表在复制后修改并没有影响新列表,说明新列表在复制的时候把嵌套列表也复制了一份。
列表生成式
fields = ['a','b','c'] data = {'a':'abc','b':'bac','c':'cba'} print([data[x] for x in fields])#['abc', 'bac', 'cba']
等介于
a = [] for x in fields: a.append(data[x]) print(a)#['abc', 'bac', 'cba']
参考博客: