# 定义一个列表,列表虽然可以存不同类型,一般我们把相同类型的值存列表里面,不同类型存字典里(key,value)
infos_list=["C#","JavaScript"]#定一个空列表 list=[]
# for遍历
for item in infos_list:
print(item)
C# JavaScript
# while遍历
i=0
while i<len(infos_list):
print(infos_list[i])
i+=1
C# JavaScript
######################## list添加系列 ##################################
# 添加~末尾追加
infos_list.append("Java")
print(infos_list)
['C#', 'JavaScript', 'Java']
# 添加~指定位置插入
infos_list.insert(0,"Python")
print(infos_list)
# 列表嵌套(后面会有扩展)
temp_list=["test1","test2"]
infos_list.insert(0,temp_list)
print(infos_list)
['Python', 'C#', 'JavaScript', 'Java'] [['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java']
infos_list #查看下现在列表是什么
[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java']
# 添加一个列表
infos_list2=["张三",21]#python里面的列表类似于List<object>
infos_list.extend(infos_list2)
print(infos_list)
[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java', '张三', 21]
#可以查看etend方法描述
help(infos_list.extend)
Help on built-in function extend: extend(...) method of builtins.list instance L.extend(iterable) -> None -- extend list by appending elements from the iterable
######################## list删除系列 ##################################
# 删除
# pop()删除最后一个元素,返回删掉的元素
infos_list.pop()
21
infos_list #查看一下列表
[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java', '张三']
# 删除
# pop(index) 删除指定下标元素,返回删掉的元素
infos_list.pop(0)
['test1', 'test2']
infos_list #查看一下列表
['Python', 'C#', 'JavaScript', 'Java', '张三']
infos_list.pop(10) #索引不存在就报错
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-15-7b0462598430> in <module>() ----> 1 infos_list.pop(10) #索引不存在就报错 IndexError: pop index out of range
# remove("")删除指定元素
infos_list.remove("张三") #没有返回值
print(infos_list)
['Python', 'C#', 'JavaScript', 'Java']
infos_list.remove("dnt") #不存在就报错
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-17-924368be629f> in <module>() ----> 1 infos_list.remove("dnt") #不存在就报错 ValueError: list.remove(x): x not in list
# del xxx[index] 删除指定下标元素
del infos_list[1] #没有返回值
print(infos_list)
['Python', 'JavaScript', 'Java']
del infos_list[10] #不存在就报错
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-19-b6366d96a6e9> in <module>() ----> 1 del infos_list[10] #不存在就报错 IndexError: list assignment index out of range
del infos_list #删除集合(集合再访问就不存在了)
infos_list #集合再访问就不存在了
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-21-97a6c19768d6> in <module>() ----> 1 infos_list #集合再访问就不存在了 NameError: name 'infos_list' is not defined
######################## list修改系列 ##################################
# 修改 xxx[index]=xx
# 注意:一般不推荐在for循环里面修改
infos_list2 #查看list2列表
['张三', 21]
infos_list2[1]="PHP" #只有下标修改一种方式
print(infos_list2)
['张三', 'PHP']
infos_list2[3]="GO" #不存在则异常
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-24-ecf5fb72864d> in <module>() ----> 1 infos_list2[3]="GO" #不存在则异常 IndexError: list assignment index out of range
# 想按值修改需要先查下标再修改
infos_list2.index("张三")
infos_list2[0]="GO"
print(infos_list2)
['GO', 'PHP']
infos_list2.index("dnt")#不存在则异常
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-26-6c57bb050f66> in <module>() ----> 1 infos_list2.index("dnt")#不存在则异常 ValueError: 'dnt' is not in list
# 知识面拓展: https://www.zhihu.com/question/49098374
# 为什么python中不建议在for循环中修改列表?
# 由于在遍历的过程中,删除了其中一个元素,导致后面的元素整体前移,导致有个元素成了漏网之鱼。
# 同样的,在遍历过程中,使用插入操作,也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。
# 如果使用while,则可以在面对这样情况的时候灵活应对。
######################## list查询系列 ##################################
# 查询 in, not in, index, count
# for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse
names_list=["张三","李四","王二麻子"]
#张三在列表中执行操作
if "张三" in names_list:
names_list.remove("张三")
print(names_list)
['李四', '王二麻子']
#查看"大舅子"不在列表中执行操作
if "大舅子" not in names_list:
names_list.append("大舅子")
print(names_list)
['李四', '王二麻子', '大舅子']
#查询王二麻子的索引
print(names_list.index("王二麻子"))
1
# 统计
print(names_list.count("大舅子"))
print(names_list.count("逆天"))
1 0
######################## list排序系列 ##################################
# 排序(sort, reverse 逆置)
num_list=[1,3,5,88,7]
#倒序
num_list.reverse()
print(num_list)
[7, 88, 5, 3, 1]
# 从小到大排序
num_list.sort()
print(num_list)
[1, 3, 5, 7, 88]
# 从大到小
num_list.sort(reverse=True)
print(num_list)
[88, 7, 5, 3, 1]
######################## list扩展系列 ##################################
# #列表嵌套(列表也是可以嵌套的)
num_list2=[33,44,22]
num_list.append(num_list2)
print(num_list)
[88, 7, 5, 3, 1, [33, 44, 22]]
# 输出
print(num_list[5])
print(num_list[5][1]) #嵌套列表获取值的方式
[33, 44, 22] 44
# 引入Null==>None
a=[1,2,3,4]
b=[5,6]
a=a.append(b)#a.append(b)没有返回值
print(a)#None
None
# range扩展~创建一个整数列表
# range(5)生成的序列是从0开始小于5的整数~[0,5)
range_list=list(range(5))
print(range_list)
[0, 1, 2, 3, 4]
# range(1,5)生成的序列是从1开始小于5的整数~[1,5)
range_list=list(range(1,5))
print(range_list)
[1, 2, 3, 4]
###################### List切片系列 ##############################
# 列表的切片操作很有用,主要跟数据相关,实际应用中和dict(后面会讲)联合使用
# python切片语法:[start_index:end_index:step] (end_index取不到)
top100=list(range(1,101)) #[1,101) => 1~100
print(top100)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
# 取前10个元素
top100[:10] #等价于:top100[0:10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 取最后10个元素
top100[-10:]
[91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
# 前11~20(eg:第二页)
top100[10:20]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# 取80~90(eg:倒数第二页)
top100[-20:-10]
[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
# 前20个数,每两个取一个(eg:隔行换样式)
top100[:20:2]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
# 所有数每10个取一个(eg:test的时候十里挑一)
top100[::10]
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
# 补充概念,str和tuple 也可以用切片操作哦~str上次说了,这次说下Tuple
# 取前两个 返回元组
(1,2,3,4,5)[:2]
(1, 2)