Python12之列表3(列表常用操作符,以及常用的内置函数)

一、比较操作符:

  list1 > list2             比较两个列表的大小,Python会直接从列表的第0个元素进行比较,若相等,则继续下一个比较

  得到的结果是布尔值

二、逻辑操作符  

1 List1 = [123,234]
2 list2 = [123,345]
3 List1 < list2
4 True
5 
6 List1 and list2
7 [123, 345]
8 list2 and List1
9 [123, 234]
View Code

三、拼接操作符

  直接使用“+”连接两个列表,注:“+”两边都必须是列表

四、重复操作符

  以*进行列表的元素重复操作

1 list2 = [123,345]
2 list2*3
3 [123, 345, 123, 345, 123, 345]
View Code

五、成员关系操作符

  in 和 not in

  注:访问列表中的列表元素,须指出具体列表元素的位置,见以下代码

1 list2 = ['1232','23','柯珂',['adf',32]]
2 '23' in list2
3 True
4 32 in list2
5 False
6 32 in list2[3]
7 True
View Code

六、列表内置方法

  clear()、copy()、count()、index()、reverse()、sort()

  clear():清空列表

  copy():拷贝列表,注意与列表赋值的区别

  count(参数):统计该参数在列表中出现的次数

  reverse():将列表中的所有元素倒置

  index(value,start,stop):判断开始到结束该value的位置值

  sort(key=None,reverse=True/Flase):当为False时,则从小到大排序,为True则从大到小排序

七、列表推导式

  (有关A的表达式 for A in B)

1 list1 = [x**3 for x in range(5)]
2 lsit1
3 [0, 1, 8, 27, 64]
View Code

猜你喜欢

转载自www.cnblogs.com/ksht-wdyx/p/11313395.html