前言
前面我们学习了 Python学习篇(一) 新建Python文件 Python学习篇(二) Python中的变量和数据类型 Python学习篇(三) Python中的运算符 Python学习篇(四) Python中的分支结构 Python学习篇(五) Python中的循环 今天我们继续学习Python中的列表,并了解列表的用途,及相关操作,就是增删改查。
一、为什么需要列表
 
二、列表的索引
列表的索引有正序和倒序两种,正序从左到右就跟数组一样,从0开始,倒序,从右往左是-1开始的。 
三、列表的查询操作

3.1 获取列表的多个元素


3.2 判断列表元素是否存在
 
四、列表元素的添加操作
没有创建新的列表对象,因为标识不变 
4.1 append

4.2 extend

4.3 insert操作

4.4 切片
五、列表的删除操作
  
list=[10,20,30,40,50,60,70,80,90,30]
list.remove(30)
print(list)
list.pop(1)
print(list)
list.pop()
print(list)
print('切片')
new_list=list[1:3]
print(new_list)
'''不产生新的列表对象,而是删除原列表的内容'''
list[1:3]=[]
print(list)
'''清除列表元素'''
list.clear()
print(list)
'''清除列表对象'''
del list
六、列表的修改操作
 
list=[10,20,30,40,50,60,70,80,90,100]
list[2]=555
print(list)
list[1:3]=[666]
print(list)
七、列表的排序操作
 
list=[10,20,30,40,50,60,70,80,90,0]
print(list,id(list))
list.sort()
print(list,id(list))
list.sort(reverse=True)
print(list,id(list))
print('-------使用内置函数sorted(),将产生一个新列表')
list=[10,20,30,40,50,60,70,80,90,0]
list=sorted(list)
print(list,id(list))
list=sorted(list,reverse=True)
print(list,id(list))
八、列表生成式
简称生成列表的公式  
list=[i for i in range(1,10)]
print(list)
'''求1到10的偶数'''
list2=[i*2 for i in range(1,6)]
print(list2)
总结

|