Python 从多个不同长度的列表中获取唯一对
Python getting unique pairs from multiple lists of different lengths
假设在 Python 中我有 3 个列表:a, b, c
可变长度。例如:
a=[1,2,3]
b=[4,5,6]
c=[7,8]
我想获得上面 3 个列表中两个元素的每个唯一组合,即。 e.
[1,4],[1,5],[1,6],[1,7],[1,8],[2,4],[2,5]...
和 3 个列表的非唯一组合(例如 [1,4,7],[1,4,8],...
)。
我已经使用 itertools
查看了 solution here,这对 2 个列表来说非常好;但是,当包含第 n
个列表时,此解决方案不再有效,因为唯一组合的长度为 n
.
这是我尝试过的:
import itertools
a=[1,2,3]
b=[4,5,6]
c=[7,8]
d=list(itertools.product(a,b,c))
[(1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (1, 6, 7), (1, 6, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (2, 6, 7), (2, 6, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8), (3, 6, 7), (3, 6, 8)]
注意:以上只是一个示例,解决方案应该适用于 n
可变长度的列表,并且可能在不同的列表中具有相同的值...任何关于我该怎么做的想法都会非常有用赞赏! :)
编辑:正如@SirParselot 所要求的,元素必须来自不同的列表
我猜你应该做的是将你的工作解决方案用于两个列表来完成 n
列表。基本上,您可以将输入转换为列表列表,然后执行:
for index, left_list in enumerate(list_of_lists):
other_lists = (list_of_lists[0:index] + list_of_lists[index+1:])
right_list = [i for sublist in other_lists for i in sublist]
print(list(itertools.product(left_list, right_list)))
您希望 Cartesian product of each pair of lists in (a, b, c)
, so first you need itertools.combinations
to generate the pairs of lists, and then itertools.product
创建所需的输出元组。
from itertools import combinations, product
def pairs(*lists):
for t in combinations(lists, 2):
for pair in product(*t):
yield pair
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8]
for pair in pairs(a, b, c):
print(pair)
输出
(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
(1, 7)
(1, 8)
(2, 7)
(2, 8)
(3, 7)
(3, 8)
(4, 7)
(4, 8)
(5, 7)
(5, 8)
(6, 7)
(6, 8)
这是一个处理重复元素的新版本。如果元组中的两个项目彼此相等,它就不是 return 元组,并且它还通过将 pairs
的输出馈送到集合中来消除输出中的重复元组。这是相当有效的,因为 pairs
是一个生成器,所以重复项会在找到时被删除。
from itertools import combinations, product
def pairs(*lists):
for t in combinations(lists, 2):
for pair in product(*t):
#Don't output pairs containing duplicated elements
if pair[0] != pair[1]:
yield pair
a = [1, 2, 3]
b = [3, 4, 5]
c = [5, 6]
#Feed the output of `pairs` into a set to eliminate duplicate tuples
output = set(pairs(a, b, c))
for pair in sorted(output):
print(pair)
输出
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 3)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
(4, 5)
(4, 6)
(5, 6)
假设在 Python 中我有 3 个列表:a, b, c
可变长度。例如:
a=[1,2,3]
b=[4,5,6]
c=[7,8]
我想获得上面 3 个列表中两个元素的每个唯一组合,即。 e.
[1,4],[1,5],[1,6],[1,7],[1,8],[2,4],[2,5]...
和 3 个列表的非唯一组合(例如 [1,4,7],[1,4,8],...
)。
我已经使用 itertools
查看了 solution here,这对 2 个列表来说非常好;但是,当包含第 n
个列表时,此解决方案不再有效,因为唯一组合的长度为 n
.
这是我尝试过的:
import itertools
a=[1,2,3]
b=[4,5,6]
c=[7,8]
d=list(itertools.product(a,b,c))
[(1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (1, 6, 7), (1, 6, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (2, 6, 7), (2, 6, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8), (3, 6, 7), (3, 6, 8)]
注意:以上只是一个示例,解决方案应该适用于 n
可变长度的列表,并且可能在不同的列表中具有相同的值...任何关于我该怎么做的想法都会非常有用赞赏! :)
编辑:正如@SirParselot 所要求的,元素必须来自不同的列表
我猜你应该做的是将你的工作解决方案用于两个列表来完成 n
列表。基本上,您可以将输入转换为列表列表,然后执行:
for index, left_list in enumerate(list_of_lists):
other_lists = (list_of_lists[0:index] + list_of_lists[index+1:])
right_list = [i for sublist in other_lists for i in sublist]
print(list(itertools.product(left_list, right_list)))
您希望 Cartesian product of each pair of lists in (a, b, c)
, so first you need itertools.combinations
to generate the pairs of lists, and then itertools.product
创建所需的输出元组。
from itertools import combinations, product
def pairs(*lists):
for t in combinations(lists, 2):
for pair in product(*t):
yield pair
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8]
for pair in pairs(a, b, c):
print(pair)
输出
(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
(1, 7)
(1, 8)
(2, 7)
(2, 8)
(3, 7)
(3, 8)
(4, 7)
(4, 8)
(5, 7)
(5, 8)
(6, 7)
(6, 8)
这是一个处理重复元素的新版本。如果元组中的两个项目彼此相等,它就不是 return 元组,并且它还通过将 pairs
的输出馈送到集合中来消除输出中的重复元组。这是相当有效的,因为 pairs
是一个生成器,所以重复项会在找到时被删除。
from itertools import combinations, product
def pairs(*lists):
for t in combinations(lists, 2):
for pair in product(*t):
#Don't output pairs containing duplicated elements
if pair[0] != pair[1]:
yield pair
a = [1, 2, 3]
b = [3, 4, 5]
c = [5, 6]
#Feed the output of `pairs` into a set to eliminate duplicate tuples
output = set(pairs(a, b, c))
for pair in sorted(output):
print(pair)
输出
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 3)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
(4, 5)
(4, 6)
(5, 6)