python面试题5---列表

mac2022-06-30  19

1.已知 AList = [1,2,3,1,2],对 AList 列表元素去重,写出具体过程 list(set(AList))

2.如何实现 “1,2,3” 变成 [“1”,“2”,“3”] “1,2,3” .split(’,’)

3.给定两个 list,A 和 B,找出相同元素和不同元素

A = ['a', 'b', 'c'] B = ['a', 'd', 'c'] print(set(A) & set(B)) print(set(A) ^ set(B))

4.[[1,2],[3,4],[5,6]]一行代码展开该列表,得出[1,2,3,4,5,6]

print([j for i in [[1, 2], [3, 4], [5, 6]] for j in i]) print([j for i in range(len([[1, 2], [3, 4], [5, 6]])) for j in [[1, 2], [3, 4], [5, 6]][i]])

5.合并列表[1,5,7,9]和[2,2,6,8] [1,5,7,9] + [2,2,6,8]

6.如何打乱一个列表的元素? random.shuffle(列表) # 返回值为None

最新回复(0)