列表3 常用操作符
程序员文章站
2022-06-14 14:32:46
...
比较操作符
逻辑操作符
连接操作符
>>> list1 = [123]
>>> list1
[123]
>>> list2 = [234]
>>> list2
[234]
>>> list1 > list2
False
>>> list1 = [123, 456]
>>> list2 = [234,345]
>>> list1 > list2
False
>>> (list1 < list2) and (list1 > list2)
False
>>> list3 = [123, 456]
>>> list3
[123, 456]
>>> (list1 < list2) and (list1 == list3)
True
只要第0个元素比较即可,后面的不管,字符串比较的话就是比较ASCII码的大小
列表也可以相加
>>> list4 = list1 + list2
>>> list4
[123, 456, 234, 345]
这个相当于之前所学习的append与extend扩展列表,但是不同的是只能添加相同类型的元素
>>> list4 + 'CCIE'
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
list4 + 'CCIE'
TypeError: can only concatenate list (not "str") to list
重复操作符
成员关系操作符
in只能判断列表内部的是否存在,如果列表中有另外一个列表,那么in是不能进行判断的,后面的操作就类似于数组的查询了
>>> 123 in list1
True
>>> 123 not in list1
False
>>> list5 = [123,['HCIA','HCIP'],456]
>>> 'HICA' in list5
False
>>> 'HCIA' in list5 [1]
True
>>> list5 [1][0]
'HCIA'
>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> list4 = list4 *4
>>> list4
[123, 456, 234, 345, 123, 456, 234, 345, 123, 456, 234, 345, 123, 456, 234, 345]
>>> list4.count(123)
4
>>> list4
[123, 456, 234, 345, 123, 456, 234, 345, 123, 456, 234, 345, 123, 456, 234, 345]
>>> list4.index(456)
1
>>> list4.index(456,3,9)
5
count返还出现次数
index返回第一次出现的位置,后面两个参数为起始范围与终止范围
reverse 颠倒顺序
>>> list4
[123, 456, 234, 345, 123, 456, 234, 345, 123, 456, 234, 345, 123, 456, 234, 345]
>>> list4.reverse()
>>> list4
[345, 234, 456, 123, 345, 234, 456, 123, 345, 234, 456, 123, 345, 234, 456, 123]
sort排序,reverse默认是false,表示顺序,true为逆序
>>> list4
[345, 234, 456, 123, 345, 234, 456, 123, 345, 234, 456, 123, 345, 234, 456, 123]
>>> list4.sort()
>>> list4
[123, 123, 123, 123, 234, 234, 234, 234, 345, 345, 345, 345, 456, 456, 456, 456]
>>> list4.sort(reverse = True)
>>> list4
[456, 456, 456, 456, 345, 345, 345, 345, 234, 234, 234, 234, 123, 123, 123, 123]
课后题
0. 注意,这道题跟上节课的那道题有点儿不同,回答完请上机实验或参考答案。
old = [1, 2, 3, 4, 5]
new = old
old = [6]
print(new)
打印 【1,2,3,4,5】
>>> old = [1,2,3,4,5]
>>> new =old
>>> old =[6]
>>> print(new)
[1, 2, 3, 4, 5]
- 请问如何将下边这个列表的’CCNA’修改为’HICA’?
list1 = [1, [1, 2, [‘CCNA’]], 3, 5, 8, 13, 18]
>>> list1 = [1, [1, 2, ['CCNA']], 3, 5, 8, 13, 18]
>>> list1[1][2] = 'HICA'
>>> list1
[1, [1, 2, 'HICA'], 3, 5, 8, 13, 18]
-
要对一个列表进行顺序排序,请问使用什么方法
解:sort() -
要对一个列表进行逆序排序,请问使用什么方法?
解:sort(reverse=Ture)
或者
列表名.sort()
列表名.reverse()
- copy() 和 clear()
clear() 方法用于清空列表的元素,但要注意,清空完后列表仍然还在哦,只是变成一个空列表
>>> list2.copy()
[234, 345]
>>> list1=list2.copy()
>>> list1
[234, 345]
>>> list2.clear()
>>> list2
[]
- 列表推导式或列表解析吗?
>>> [i*i for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
分别打印了0到9各个数的平方,然后还放在列表里
列表推导式(List comprehensions)也叫列表解析,灵感取自函数式编程语言 Haskell。Ta 是一个非常有用和灵活的工具,可以用来动态的创建列表,语法如
[有关A的表达式 for A in B]
>>> list1 = [x**x for x in range(10)]
>>> list1
[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]
相当于
>>> list1=[]
>>> for x in range(10):
temp =x**x
list1.append(temp)
>>> list1
[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]
list1 = [(x, y) for x in range(10) for y in range(10) if x%2==0 if y%2!=0]还原为原来的式子
>>> list1 = []
>>> list1 = [(x, y) for x in range(10) for y in range(10) if x%2==0 if y%2!=0]
>>> list1
[(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)]
相当于
>>> list1= []
>>> for x in range(10):
for y in range(10):
if x%2 ==0:
if y%2 !=0:
list1.append((x,y))
>>> list1
[(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)]
- 活学活用
list3 = [name + ‘:’ + slogan[2:] for slogan in list1 for name in list2 if slogan[0] == name[0]]