Python intersection of a plurality of sets, and lists, and difference (complement)

1. seeking plurality intersection list

Input:

"""以a、b、c为实验对象,求a、b、c都同时拥有的元素"""
a = [0,1,2,3,4]
b = [0,2,6]
c = [-1,2,5,8]

# 求多个list的交集:a、b、c同时拥有的元素
r = list(set(a).intersection(b,c)) 
print('r -->', r)   # 输出r --> [2]

2. The list of a plurality of seek and set

Input:

"""以a、b、c为实验对象,求a、b、c的并集"""
a = [0,1,2,3,4]
b = [0,2,6]
c = [-1,2,5,8]

# 求多个list的并集
r = list(set(a).union(b,c)) 
print('r -->', r)   # 输出:r --> [0, 1, 2, 3, 4, 5, 6, 8, -1]

3. A list of the plurality of seek difference (s) set - i.e., retrieve a specific list has an other list elements are not

Input:

"""以a、b、c为实验对象,求 a 中有,但 b 和 c 都没有的元素的并集"""
a = [0,1,2,3,4]
b = [0,2,6]
c = [-1,2,5,8]

# 求特定1个list(a)中有,其他list(b、c)都没有的元素
r = list(set(a).difference(b,c))
print('r -->', r)   # 输出:r --> [1, 3, 4]
Published 146 original articles · won praise 66 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_38923792/article/details/103605178