dho*_*hop 6 python list unique set
我试图找到5个不同列表之间的独特区别。
我已经看到了多个如何查找两个列表之间差异的示例,但无法将其应用于多个列表。
找到5个列表之间的相似点很容易。
例:
list(set(hr1) & set(hr2) & set(hr4) & set(hr8) & set(hr24))
Run Code Online (Sandbox Code Playgroud)
但是,我想弄清楚如何确定每个集合的独特功能。
有谁知道如何做到这一点?
这个怎么样?假设我们有输入列表[1, 2, 3, 4]、[3, 4, 5, 6]和[3, 4, 7, 8]。[1, 2]我们想要从第一个列表、[5, 6]第二个列表和[7, 8]第三个列表中取出。
from itertools import chain
A_1 = [1, 2, 3, 4]
A_2 = [3, 4, 5, 6]
A_3 = [3, 4, 7, 8]
# Collect the input lists for use with chain below
all_lists = [A_1, A_2, A_3]
for A in (A_1, A_2, A_3):
# Combine all the lists into one
super_list = list(chain(*all_lists))
# Remove the items from the list under consideration
for x in A:
super_list.remove(x)
# Get the unique items remaining in the combined list
super_set = set(super_list)
# Compute the unique items in this list and print them
uniques = set(A) - super_set
print(sorted(uniques))
Run Code Online (Sandbox Code Playgroud)