Python 比较列表并找到组合
我有3个列表,我试图找到匹配的组合Python 比较列表并找到组合,python,Python,我有3个列表,我试图找到匹配的组合 mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"] mylist2 = ["a", "b", "c", "d", "p",
mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]
g、 h、x和y没有任何匹配项,因此我将丢弃它们。结果[a,b,c]3是有效的,但我也需要放弃它,因为它是[a,b,c,d]3的子集。预期输出为:
["a", "b", "c", "d"] 3
["a", "b", "c", "d", "e", "f"] 2
["a", "b", "c", "d", "p"] 2
["a", "b", "c", "d", "q"] 2
输出:
{'a', 'b', 'd', 'c'} 3
{'d', 'c', 'a', 'p', 'b'} 2
{'d', 'f', 'c', 'a', 'e', 'b'} 2
{'d', 'c', 'a', 'b', 'q'} 2
输出:
{'a', 'b', 'd', 'c'} 3
{'d', 'c', 'a', 'p', 'b'} 2
{'d', 'f', 'c', 'a', 'e', 'b'} 2
{'d', 'c', 'a', 'b', 'q'} 2
这将使您获得预期的输出
mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]
s1 = set(mylist1)
s2 = set(mylist2)
s3 = set(mylist3)
print(sorted(list(s1.intersection(s2).intersection(s3))), 3)
print(sorted(list(s1.intersection(s3))), 2)
print(sorted(list(s1.intersection(s2))), 2)
print(sorted(list(s2.intersection(s3))), 2)
首先,将列表转换为set。
然后与集合进行交集,然后将其转换为列表,然后对其进行排序。这将获得预期的输出
mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]
s1 = set(mylist1)
s2 = set(mylist2)
s3 = set(mylist3)
print(sorted(list(s1.intersection(s2).intersection(s3))), 3)
print(sorted(list(s1.intersection(s3))), 2)
print(sorted(list(s1.intersection(s2))), 2)
print(sorted(list(s2.intersection(s3))), 2)
首先,将列表转换为set。
然后与集合相交,然后将其转换为列表,然后对其进行排序。假设您的要求是: 你不想看到任何只发生一次的事情- 但只想显示至少在两个列表中常见的内容 首先,您需要计算出可以从列表中选择多少组合。 这里有3个列表->即4个组合-itertools.combinations可以帮助您实现这一点 然后,您需要定义这些组合,并将它们逐一相交。请参见下文:
import itertools
from functools import reduce
mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]
def definer(*args):
# Number of lists for input
counter = len(args)
my_outputs = []
# Only collecting where values are at least in two lists:
for i in range(2, counter+1):
x = (g for g in itertools.combinations(args, i))
for item in x:
result = reduce(set.intersection, (set(a) for a in item))
my_outputs.append([sorted(list(result)), i])
return my_outputs
print(definer(mylist1,mylist2,mylist3))
假设您的需求是: 你不想看到任何只发生一次的事情- 但只想显示至少在两个列表中常见的内容 首先,您需要计算出可以从列表中选择多少组合。 这里有3个列表->即4个组合-itertools.combinations可以帮助您实现这一点 然后,您需要定义这些组合,并将它们逐一相交。请参见下文:
import itertools
from functools import reduce
mylist1 = ["a", "b", "c", "d", "e", "f", "x", "y", "p"]
mylist2 = ["a", "b", "c", "d", "p", "q"]
mylist3 = ["a", "b", "c", "d", "e", "f", "g", "h", "q"]
def definer(*args):
# Number of lists for input
counter = len(args)
my_outputs = []
# Only collecting where values are at least in two lists:
for i in range(2, counter+1):
x = (g for g in itertools.combinations(args, i))
for item in x:
result = reduce(set.intersection, (set(a) for a in item))
my_outputs.append([sorted(list(result)), i])
return my_outputs
print(definer(mylist1,mylist2,mylist3))
进口itertools
def mainT0:列表[设置]:
对于范围2中的i,lenT0+1:
对于itertools.combinationsT0中的o1,i:
t1=o1[0]。是否复制
对于o1[1:]中的o2:
t1&=o2
n1=0
对于T0中的o3:
n1+=t1.3
打印1,n1
主要[
{a,b,c,d,e,f,x,y,p},
{a,b,c,d,p,q},
{a,b,c,d,e,f,g,h,q},
]
进口itertools
def mainT0:列表[设置]:
对于范围2中的i,lenT0+1:
对于itertools.combinationsT0中的o1,i:
t1=o1[0]。是否复制
对于o1[1:]中的o2:
t1&=o2
n1=0
对于T0中的o3:
n1+=t1.3
打印1,n1
主要[
{a,b,c,d,e,f,x,y,p},
{a,b,c,d,p,q},
{a,b,c,d,e,f,g,h,q},
]
为了确保我读对了这篇文章,您需要包含至少两个数组中的字母的最大集合吗?你保证每件物品在每个阵法中最多出现一次吗?@Jon是的,是的。你的问题是什么?你想问怎么做吗?如果是这样,您已经尝试过什么?如果我在mylist3中交换b和c,预期的输出是什么?输出将是相同的。我的列表包含唯一的排序值。为了确保我读对了,您想要包含至少两个数组中的字母的最大集合吗?你保证每件物品在每个阵法中最多出现一次吗?@Jon是的,是的。你的问题是什么?你想问怎么做吗?如果是这样,您已经尝试过什么?如果我在mylist3中交换b和c,预期的输出是什么?输出将是相同的。我的列表包含唯一的、排序的值。如果我有几百个(如果不是几千个)列表,它会扩展吗?这是原始数据的样本。我是否应该将每个集合与增量为2,3,4…的所有其他集合进行比较?对于大型数据集,我认为如果我有几百个列表(如果不是数千个),您应该使用它是否可以扩展?这是原始数据的样本。我是否应该将每个集合与增量为2,3,4…的所有其他集合进行比较?对于大型数据集,我认为应该使用