List(列表)其他语言是数组
用 [ ] 定义 数据之间用,间隔 索引从0开始
超出索引范围会报错
name=["zhangsan","lisi","wnagwu"] #用,隔开
print(name[0])
列表常用操作
列表.index(self,object对象,start,stop) 返回内容索引
name_list=["123","456","789"]
print(name_list[2])
print(name_list.index("789"))#返回索引
name_list[1]="zhangsan"
print(name_list[1])
#name_list[3]="asd"超出范围会报错
name_list.append("wangxiaoer")#追加
print(name_list)
name_list.insert(1,"xiaomeimei")#插入
print(name_list)
name_list.extend(("laoda","laoer","laosan"))#连接内容列表
print(name_list)
temp_liae=["laoda","laoer","laosan"]
print(temp_liae)
name_list.extend(temp_liae)#连接列表名
print(name_list)
----------------
789
2
zhangsan
['123', 'zhangsan', '789', 'wangxiaoer']
['123', 'xiaomeimei', 'zhangsan', '789', 'wangxiaoer']
['123', 'xiaomeimei', 'zhangsan', '789', 'wangxiaoer', 'laoda', 'laoer', 'laosan']
['laoda', 'laoer', 'laosan']
['123', 'xiaomeimei', 'zhangsan', '789', 'wangxiaoer', 'laoda', 'laoer', 'laosan', 'laoda', 'laoer', 'laosan']
增加
列表.insert(索引,数据) 在指定位置插入数据
列表.append(数据) 在末尾追加数据
列表.extend(列表2) 将列表2的数据追加到列表
修改
列表[索引]=数据 修改指定索引的数据
删除
del 列表[索引] 删除指定索引的数据(内存中删除)
#del delete删除 变量从内存删除
#在开发中,用删除列表方法,不用del
name_list=["123","456","789"]
del name_list[1]
name="xiao"
del name
print(name)#从内存删除后序代码不能使用
print(name_list)
列表.remove[数据] 删除第一个出现的指定数据
列表.pop 删除末尾数据
列表.pop(索引) 删除指定索引数据
列表.clear 清空列表
name_list=["123","456","789"]
print(name_list[2])
print(name_list.index("789"))#返回索引
name_list[1]="zhangsan"
print(name_list[1])
#name_list[3]="asd"超出范围会报错
name_list.append("wangxiaoer")#追加
print(name_list)
name_list.insert(1,"xiaomeimei")#插入
print(name_list)
name_list.extend(("laoda","laoer","laosan"))#连接内容列表
print(name_list)
temp_liae=["laoda","laoer","laosan"]
print(temp_liae)
name_list.extend(temp_liae)#连接列表名
print(name_list)
name_list.remove("xiaomeimei")#删除
print(name_list)
name_list.pop()#默认删除最后一个元素
print(name_list)
name_list.pop(3)#删除指定索引
print(name_list)
name_list.clear()
print(name_list)
----------------
D:\Anaconda\python.exe D:/Pycharm/PycharmProjects/python列表/k02_列表方法.py
789
2
zhangsan
['123', 'zhangsan', '789', 'wangxiaoer']
['123', 'xiaomeimei', 'zhangsan', '789', 'wangxiaoer']
['123', 'xiaomeimei', 'zhangsan', '789', 'wangxiaoer', 'laoda', 'laoer', 'laosan']
['laoda', 'laoer', 'laosan']
['123', 'xiaomeimei', 'zhangsan', '789', 'wangxiaoer', 'laoda', 'laoer', 'laosan', 'laoda', 'laoer', 'laosan']
['123', 'zhangsan', '789', 'wangxiaoer', 'laoda', 'laoer', 'laosan', 'laoda', 'laoer', 'laosan']
['123', 'zhangsan', '789', 'wangxiaoer', 'laoda', 'laoer', 'laosan', 'laoda', 'laoer']
['123', 'zhangsan', '789', 'laoda', 'laoer', 'laosan', 'laoda', 'laoer']
[]
Process finished with exit code 0
统计
len(列表) 列表长度
列表.count(数据) 数据在列表中出现的次数
name_list=["123","456","789","123","456","123"]
list_len=len(name_list)
print("%d"%list_len)
count=name_list.count("123")#统计数据出现的次数
print("%d"%count)
name_list.remove("123")#删除第一个出现的数据
print(name_list)
----------------
D:\Anaconda\python.exe D:/Pycharm/PycharmProjects/python列表/k04_数据统计.py
6
3
['456', '789', '123', '456', '123']
Process finished with exit code 0
排列
列表.sort() 升序排序
列表.sort(reverse=Ture) 降序排序
列表.reverse() 逆序、反转
name_list=["123","456","789","zhang","sun"]
num_list=[6,10,2,40,7,8,9,]
#shengxu
name_list.sort()
print(name_list)
num_list.sort()
print(num_list)
print(name_list ,num_list)
#jiangxu
name_list.sort(reverse=True)
num_list.sort(reverse=True)
print(name_list ,num_list)
#fanzhuan
name_list.reverse()
num_list.reverse()
print(name_list ,num_list)
---------------
D:\Anaconda\python.exe D:/Pycharm/PycharmProjects/python列表/k05_排序.py
['123', '456', '789', 'sun', 'zhang']
[2, 6, 7, 8, 9, 10, 40]
['123', '456', '789', 'sun', 'zhang'] [2, 6, 7, 8, 9, 10, 40]
['zhang', 'sun', '789', '456', '123'] [40, 10, 9, 8, 7, 6, 2]
['123', '456', '789', 'sun', 'zhang'] [2, 6, 7, 8, 9, 10, 40]
Process finished with exit code 0