![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ff77bfff0a56ab755fafe952fa9ecac0.png#pic_center)
为什么需要列表?
lst=['hello','world',98]
print(id(lst))
print(type(lst))
print(lst)
2008431996736
<class 'list'>
['hello', 'world', 98]
Process finished with exit code 0
列表的创建
1
lst=['hello','world',98]
2
lst2=list(['hello','world',98])
列表的特点
获取指定元素的索引
1
lst=['hello','world',98,'hello']
print(lst.index('hello'))
0
2查找不存在于列表中的元素
lst=['hello','world',98,'hello']
print(lst.index('小贾'))
print(lst.index('小贾'))
ValueError: '小贾' is not in list
3在指定范围查找
lst=['hello','world',98,'hello']
print(lst.index('hello',1,4))
3
4获取列表当中的单个元素正向负向获取
lst=['hello','world',98,'hello','world',234]
print(lst[2])
print(lst[-3])
98
hello
5获取列表当中的多个元素
lst=[10,20,30,40,50,60,70,80]
print(lst[1:6:1])
[20, 30, 40, 50, 60]
列表元素的查询操作
列表元素的增加操作
元素的删除
列表元素的修改
1一次修改一个值
lst=[10,20,30,40,50,60,70,80]
lst[2]=100
print(lst)
[10, 20, 100, 40, 50, 60, 70, 80]
2切片法
lst=[10,20,30,40]
lst[1:3]=[300,400,500,600]
print(lst)
[10, 300, 400, 500, 600, 40]
列表的排序操作
lst=[10,20,30,40,5,4,6,3]
print('排序前的列表',lst,id(lst))
lst.sort()
print('排序后的列表',lst,id(lst))
排序前的列表 [10, 20, 30, 40, 5, 4, 6, 3] 1761440247744
排序后的列表 [3, 4, 5, 6, 10, 20, 30, 40] 1761440247744
升序降序
lst=[10,20,30,40,5,4,6,3]
print('排序前的列表',lst,id(lst))
lst.sort()
print('排序后的列表',lst,id(lst))
lst.sort(reverse=True)
print(lst)
lst.sort(reverse=False)
print(lst)
排序前的列表 [10, 20, 30, 40, 5, 4, 6, 3] 2559156039616
排序后的列表 [3, 4, 5, 6, 10, 20, 30, 40] 2559156039616
[40, 30, 20, 10, 6, 5, 4, 3]
[3, 4, 5, 6, 10, 20, 30, 40]
列表生成式
列表总结
焦虑了,我怎么可能需要到啥再去学啥,一直以来不是学了啥之后用啥嘛,难道还得思维转变转变?……那——学学相当于文化熏陶吧