如何将长列表的排列与短列表的排列进行匹配(根据短列表的长度)?[重复]
- 2025-01-16 08:38:00
- admin 原创
- 92
问题描述:
我无法理解我尝试实现的算法。我有两个列表,想从这两个列表中获取特定的组合。
这是一个例子。
names = ['a', 'b']
numbers = [1, 2]
在这种情况下,输出将是:
[('a', 1), ('b', 2)]
[('b', 1), ('a', 2)]
我的名字可能比号码多,即len(names) >= len(numbers)
。以下是包含 3 个名字和 2 个号码的示例:
names = ['a', 'b', 'c']
numbers = [1, 2]
输出:
[('a', 1), ('b', 2)]
[('b', 1), ('a', 2)]
[('a', 1), ('c', 2)]
[('c', 1), ('a', 2)]
[('b', 1), ('c', 2)]
[('c', 1), ('b', 2)]
解决方案 1:
最简单的方法是使用itertools.product
:
a = ["foo", "melon"]
b = [True, False]
c = list(itertools.product(a, b))
>> [("foo", True), ("foo", False), ("melon", True), ("melon", False)]
解决方案 2:
可能比上面最简单的一个更简单:
>>> a = ["foo", "bar"]
>>> b = [1, 2, 3]
>>> [(x,y) for x in a for y in b] # for a list
[('foo', 1), ('foo', 2), ('foo', 3), ('bar', 1), ('bar', 2), ('bar', 3)]
>>> ((x,y) for x in a for y in b) # for a generator if you worry about memory or time complexity.
<generator object <genexpr> at 0x1048de850>
无需任何进口
解决方案 3:
注意:此答案针对的是上面提出的具体问题。如果您来自 Google,并且只是想在 Python 中寻找一种获取笛卡尔积的方法,itertools.product
或者您可能正在寻找简单的列表理解 - 请参阅其他答案。
假设len(list1) >= len(list2)
。那么您似乎想要的是取出长度为 的所有排列len(list2)
,list1
并将它们与 list2 中的项目进行匹配。在 Python 中:
import itertools
list1=['a','b','c']
list2=[1,2]
[list(zip(x,list2)) for x in itertools.permutations(list1,len(list2))]
返回
[[('a', 1), ('b', 2)], [('a', 1), ('c', 2)], [('b', 1), ('a', 2)], [('b', 1), ('c', 2)], [('c', 1), ('a', 2)], [('c', 1), ('b', 2)]]
解决方案 4:
我正在寻找一个仅包含唯一组合的自身相乘列表,该列表由此函数提供。
import itertools
itertools.combinations(list, n_times)
这里是Python 文档 itertools
的摘录,它可能会帮助您找到您要查找的内容。
Combinatoric generators:
Iterator | Results
-----------------------------------------+----------------------------------------
product(p, q, ... [repeat=1]) | cartesian product, equivalent to a
| nested for-loop
-----------------------------------------+----------------------------------------
permutations(p[, r]) | r-length tuples, all possible
| orderings, no repeated elements
-----------------------------------------+----------------------------------------
combinations(p, r) | r-length tuples, in sorted order, no
| repeated elements
-----------------------------------------+----------------------------------------
combinations_with_replacement(p, r) | r-length tuples, in sorted order,
| with repeated elements
-----------------------------------------+----------------------------------------
product('ABCD', repeat=2) | AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
permutations('ABCD', 2) | AB AC AD BA BC BD CA CB CD DA DB DC
combinations('ABCD', 2) | AB AC AD BC BD CD
combinations_with_replacement('ABCD', 2) | AA AB AC AD BB BC BD CC CD DD
解决方案 5:
找出大量列表的所有组合的最佳方法是:
import itertools
from pprint import pprint
inputdata = [
['a', 'b', 'c'],
['d'],
['e', 'f'],
]
result = list(itertools.product(*inputdata))
pprint(result)
结果是:
[('a', 'd', 'e'),
('a', 'd', 'f'),
('b', 'd', 'e'),
('b', 'd', 'f'),
('c', 'd', 'e'),
('c', 'd', 'f')]
解决方案 6:
或者简短列表的 KISS 答案:
[(i, j) for i in list1 for j in list2]
性能不如 itertools,但您使用的是 python,所以性能已经不是您的首要关注点了……
我也喜欢所有其他的答案!
解决方案 7:
您可能想尝试一行列表理解:
>>> [name+number for name in 'ab' for number in '12']
['a1', 'a2', 'b1', 'b2']
>>> [name+number for name in 'abc' for number in '12']
['a1', 'a2', 'b1', 'b2', 'c1', 'c2']
解决方案 8:
对 interjay 的答案进行了微小的改进,使结果成为一个扁平列表。
>>> list3 = [zip(x,list2) for x in itertools.permutations(list1,len(list2))]
>>> import itertools
>>> chain = itertools.chain(*list3)
>>> list4 = list(chain)
[('a', 1), ('b', 2), ('a', 1), ('c', 2), ('b', 1), ('a', 2), ('b', 1), ('c', 2), ('c', 1), ('a', 2), ('c', 1), ('b', 2)]
参考自此链接
解决方案 9:
不itertools
包含扁平列表:
[(list1[i], list2[j]) for i in range(len(list1)) for j in range(len(list2))]
或者在Python 2中:
[(list1[i], list2[j]) for i in xrange(len(list1)) for j in xrange(len(list2))]
解决方案 10:
回答问题“给定两个列表,找出每个列表中一个项目的所有可能的排列组合”,并使用基本的 Python 功能(即不使用 itertools),从而可以轻松地复制到其他编程语言:
def rec(a, b, ll, size):
ret = []
for i,e in enumerate(a):
for j,f in enumerate(b):
l = [e+f]
new_l = rec(a[i+1:], b[:j]+b[j+1:], ll, size)
if not new_l:
ret.append(l)
for k in new_l:
l_k = l + k
ret.append(l_k)
if len(l_k) == size:
ll.append(l_k)
return ret
a = ['a','b','c']
b = ['1','2']
ll = []
rec(a,b,ll, min(len(a),len(b)))
print(ll)
返回
[['a1', 'b2'], ['a1', 'c2'], ['a2', 'b1'], ['a2', 'c1'], ['b1', 'c2'], ['b2', 'c1']]
解决方案 11:
对此问题的更好答案仅适用于所提供的特定长度的列表。
这是一个适用于任意长度输入的版本。它还根据组合和排列的数学概念使算法清晰易懂。
from itertools import combinations, permutations
list1 = ['1', '2']
list2 = ['A', 'B', 'C']
num_elements = min(len(list1), len(list2))
list1_combs = list(combinations(list1, num_elements))
list2_perms = list(permutations(list2, num_elements))
result = [
tuple(zip(perm, comb))
for comb in list1_combs
for perm in list2_perms
]
for idx, ((l11, l12), (l21, l22)) in enumerate(result):
print(f'{idx}: {l11}{l12} {l21}{l22}')
输出:
0: A1 B2
1: A1 C2
2: B1 A2
3: B1 C2
4: C1 A2
5: C1 B2