合并两个或多个列表获得具有相同长度和统计信息的唯一组合
merge two or more lists get unique combinations with same length and statistics
l1=[1,1,2,3,5,6,6]
l2 = [2,2,1,1,5,6]
然后return一种可能性[(1,2),(1,1),(2,2),(3,2),(5,1),(6,5 ),(6,6) ]
所以 l1 和 l2 的相同统计数据用于独特的组合。列表数量可以是2个或更多
l1和l2的长度为6
最终结果的长度也是 6
我想过使用 itertools 模块,但是很难在每个列表中维护相同的统计信息。
import itertools
l1 = [1,1,2,3,5,6]
l2 = [2,2,1,1,5,6]
found = {}
for perm in itertools.permutations(l1, len(l1)):
pairs = set(zip(perm, l2))
if len(pairs) == len(l1):
found = pairs
break
print(found if found else "not found")
l1=[1,1,2,3,5,6,6]
l2 = [2,2,1,1,5,6]
然后return一种可能性[(1,2),(1,1),(2,2),(3,2),(5,1),(6,5 ),(6,6) ] 所以 l1 和 l2 的相同统计数据用于独特的组合。列表数量可以是2个或更多
l1和l2的长度为6 最终结果的长度也是 6
我想过使用 itertools 模块,但是很难在每个列表中维护相同的统计信息。
import itertools
l1 = [1,1,2,3,5,6]
l2 = [2,2,1,1,5,6]
found = {}
for perm in itertools.permutations(l1, len(l1)):
pairs = set(zip(perm, l2))
if len(pairs) == len(l1):
found = pairs
break
print(found if found else "not found")