# Python—列表的常用操作符

1、比较操作符

2、逻辑操作符

3、连接操作符

4、重复操作符

5、成员关系操作符

在列表的元素比较时，第一个元素起决定作用。

>>> list = [123]
>>> list2 = [234]
>>> list2 > list
True
>>> list1 = [412,123]
>>> list2 = [542,111]
>>> list1 >list2
False

>>> list3 =list1+list2
>>> list3
[412, 123, 542, 111]
>>> 

>>> list3
[412, 123, 542, 111]
>>> list3 *3
[412, 123, 542, 111, 412, 123, 542, 111, 412, 123, 542, 111]
>>> list3*=3
>>> list3
[412, 123, 542, 111, 412, 123, 542, 111, 412, 123, 542, 111]


>>> list3
[412, 123, 542, 111, 412, 123, 542, 111, 412, 123, 542, 111]
>>> 123 in list3
True
>>> 123 not in list3
False
>>> 234 in list3
False
>>> 234 not in list3
True

>>> list6 = ['567',['xiaoyi','hello'],'123']
>>> 'xiaoyi' in list6
False

>>> 'xiaoyi' in list6[1]
True


>>> list6 = ['567',['xiaoyi','hello'],'123']
>>> list6[1][1]
'hello'


>>> list3
[412, 123, 542, 111, 412, 123, 542, 111, 412, 123, 542, 111]
>>> list3.count(123)
3

>>> list3
[412, 123, 542, 111, 412, 123, 542, 111, 412, 123, 542, 111]
>>> list3.index(123,2,6)
5
>>> list3.index(123)
1

将整个列表元素翻转函数：reverse()函数

>>> list3
[412, 123, 542, 111, 412, 123, 542, 111, 412, 123, 542, 111]
>>> list3.reverse()
>>> list3
[111, 542, 123, 412, 111, 542, 123, 412, 111, 542, 123, 412]


>>> list9 = [3,2,8,5,10]
>>> list9.sort()
>>> list9
[2, 3, 5, 8, 10]
>>> list9.sort(reverse = True)
>>> list9
[10, 8, 5, 3, 2]

>>> list1 = [9,8,7,6,5,4,3,2,1]
>>> list2 = list1[:]
>>> list2
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> list3 = list1
>>> list3
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> list1.sort()
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list2
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> list3
[1, 2, 3, 4, 5, 6, 7, 8, 9]


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客