- A+
所属分类:Python学习
课程内容:
列表操作符
- 比较操作符
> < >= <= != ==
- >>> list1=['123','456']
- >>> list2=['234','456']
- >>> list1 < list2
- True
- 逻辑操作符
and or not
- >>> list3 >list1 and list3 < list2
- True
- 连接操作符
+号相连接,但不可用于向列表中新增元素
- >>> list3 = list1 +list2
- >>> list3
- ['123', '456', '234', '456']
- 重复操作符
*
- >>> list4 = list3 *3
- >>> list4
- ['123', '456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456']
- 成员操作符
in、not in
- ['123', '456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456']
- >>> 123 in list4
- False
- >>> 123 not in list4
- True
- >>> '123' in list4
- True
列表内置函数
查看列表的内置函数,可以通过dir(list)或help(list)来查看相关内容
- 列表排序
.sort(),默认从小到大进行排序
第六行直接在sort函数中,加入reverse=True,给排序置反。
- >>> list5=[1,5,3,6,8,9]
- >>> list6=['c','s','e','r','t','y']
- >>> list5.sort()
- >>> list6.sort()
- >>> print(list5,list6)
- [1, 3, 5, 6, 8, 9] ['c', 'e', 'r', 's', 't', 'y']
- >>> list6.sort(reverse=True)
- >>> list6
- ['y', 't', 's', 'r', 'e', 'c']
.reverse(),将原列表排序翻转。
- >>> list4.reverse()
- >>> list4
- ['456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456', '123']
- 列表元素统计次数
count(),计算元素在列表中的次数
- >>> list4.count('123')
- 3
- 列表位置索引
列表名.index(),返回元素在列表中的位置,除了元素名,还默认支持两个参数,用于给列表指定位置。
- >>> list4.index('123',2,10)
- 4
- copy()
- clear(),清除列表中的内容
- >>> list1= [1,2,3,4,5]
- >>> list1
- [1, 2, 3, 4, 5]
- >>> list1.clear()
- >>> list1
- []
下面的内容是课外的内容,一并记录在此:
获取列表中最大的数字,和最小的数字
- >>> list4=['123', '456', '234', '456', '123', '456', '234', '456', '123', '456', '234', '456']
- >>> min(list4)
- '123'
- >>> max(list4)
- '456'
- >>>
单词扩展:
- sort :将。。。排序
- reverse :颠倒
- count:计算
- min:最小值(minimum缩写)
- max:最大数(maximum缩写)
扩展阅读:
- 通过示例学习Python列表推导
版权注释:
Python课程来源于鱼C论坛:http://bbs.fishc.com/forum-243-1.html 版块,课程内容为免费内容,如果你喜欢该课程,建议购买VIP账号支持小甲鱼,官方网店:https://fishc.taobao.com/)。
本内容为在李金龙在学习课程中做的日记记录,方便自己以后查找相关信息,另一方面也希望自己写下的东西可以帮助到别人。
课程内容:http://blog.fishc.com/2914.html