反转列表的方式

1
2
3
4
5
6
a = [1,2,3,4,5,6]

a = a[::-1]
print(a)

[6, 5, 4, 3, 2, 1]
1
2
3
4
5
a = [1,2,3,4,5,6]
a.reverse()
print(a)

[6, 5, 4, 3, 2, 1]
1
2
3
4
5
a = [1,2,3,4,5,6]
b = sorted(a,reverse=True)
print(b)

[6, 5, 4, 3, 2, 1]
1
2
3
4
5
a = [1,2,3,4,5,6]
a.sort(reverse=True)
print(a)

[6, 5, 4, 3, 2, 1]

列表取同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a = [11,22,33]
b = [22,33,44]
c = []
for i in a: # 遍历a
if i in b: # 判断交集
c.append(i)
print(c)

print(list(set(a) & set(b)))
print(list(set(a).intersection(set(b))))

[22, 33]
[33, 22]
[33, 22]