想要将列表的每个元素与 n 个列表的每个元素组合
want to combine every element of a list with every element of n lists
我将尝试用一个例子来解释这一点,因为我似乎无法向自己解释它:
假设我有一个字符串列表和另一个字符串列表列表:
words = ["hello", "goodbye", "foo"]
lists = [["111", "450", "nice"], ["can", "be", "of", "different", "sizes"]]
我想将第一个列表中的一项与列表中 n 个列表中的一项合并,示例:
对于 n = 1:
hello111
hello450
hellonice
hellocan
hellobe
...
或 n = 2
hello111can
hello111be
hello111of
...
n = 3 在这种情况下是不可能的
我在 python 中尝试使用 itertools 使用产品或其他东西,但我似乎无法理解如何做到这一点
[编辑]
我标记为正确的答案是我想要的,但使用排列而不是组合,非常感谢!
首先,使用 itertools.combinations(lists, n)
从 lists
中获取 n
个元素的组合,然后使用 itertools.product(words, *comb)
获取原始单词与该组合中的元素的乘积.您可以将这两个步骤合并为一个 double-loop 列表理解:
>>> n = 1
>>> [x for comb in itertools.combinations(lists, n) for x in itertools.product(words, *comb)]
[('hello', '111'),
('hello', '450'),
('hello', 'nice'),
('goodbye', '111'),
...
('foo', 'sizes')]
或 n = 2
:
[('hello', '111', 'can'),
('hello', '111', 'be'),
('hello', '111', 'of'),
('hello', '111', 'different'),
('hello', '111', 'sizes'),
('hello', '450', 'can'),
...
('foo', 'nice', 'sizes')]
对于 n = 3
及以上,您将获得 []
。
最后,只是 ''.join
那些在一起。 (我没有这样做,所以它更具可读性。)
>>> [''.join(x) for comb in itertools.combinations(lists, n) for x in itertools.product(words, *comb)]
from itertools import combinations, product
words = ["hello", "goodbye", "foo"]
lists = [["111", "450", "nice"], ["can", "be", "of", "different", "sizes"]]
# how many elements of `lists` to pick from?
for n in range(1, len(lists) + 1):
# This returns in-order combinations, ie you will get
# '111', 'can' and not 'can', '111'.
# If you want all orderings as well as all combinations,
# use itertools.permutations instead,
for sublist in combinations(lists, n):
# now we generate all combinations of
# one element from each basis list,
basis = [words] + list(sublist)
for combo in product(*basis):
# and display the result
print("".join(combo))
这给出了
hello111
hello450
hellonice
goodbye111
goodbye450
goodbyenice
foo111
foo450
foonice
hellocan
hellobe
helloof
hellodifferent
hellosizes
goodbyecan
goodbyebe
goodbyeof
goodbyedifferent
goodbyesizes
foocan
foobe
fooof
foodifferent
foosizes
hello111can
hello111be
hello111of
hello111different
hello111sizes
hello450can
hello450be
hello450of
hello450different
hello450sizes
hellonicecan
hellonicebe
helloniceof
hellonicedifferent
hellonicesizes
goodbye111can
goodbye111be
goodbye111of
goodbye111different
goodbye111sizes
goodbye450can
goodbye450be
goodbye450of
goodbye450different
goodbye450sizes
goodbyenicecan
goodbyenicebe
goodbyeniceof
goodbyenicedifferent
goodbyenicesizes
foo111can
foo111be
foo111of
foo111different
foo111sizes
foo450can
foo450be
foo450of
foo450different
foo450sizes
foonicecan
foonicebe
fooniceof
foonicedifferent
foonicesizes
在 n=2、n=3 等之前生成所有 n=1。如果您不关心顺序,则可以改为
for word in words:
combos = product(*([""] + sublist for sublist in lists))
next(combos) # skip n=0
for combo in combos:
print(word + "".join(combo))
产生
hellocan
hellobe
helloof
hellodifferent
hellosizes
hello111
hello111can
hello111be
hello111of
hello111different
hello111sizes
hello450
hello450can
hello450be
hello450of
hello450different
hello450sizes
hellonice
hellonicecan
hellonicebe
helloniceof
hellonicedifferent
hellonicesizes
goodbyecan
goodbyebe
goodbyeof
goodbyedifferent
goodbyesizes
goodbye111
goodbye111can
goodbye111be
goodbye111of
goodbye111different
goodbye111sizes
goodbye450
goodbye450can
goodbye450be
goodbye450of
goodbye450different
goodbye450sizes
goodbyenice
goodbyenicecan
goodbyenicebe
goodbyeniceof
goodbyenicedifferent
goodbyenicesizes
foocan
foobe
fooof
foodifferent
foosizes
foo111
foo111can
foo111be
foo111of
foo111different
foo111sizes
foo450
foo450can
foo450be
foo450of
foo450different
foo450sizes
foonice
foonicecan
foonicebe
fooniceof
foonicedifferent
foonicesizes
(相同列表,不同顺序)。
我将尝试用一个例子来解释这一点,因为我似乎无法向自己解释它:
假设我有一个字符串列表和另一个字符串列表列表:
words = ["hello", "goodbye", "foo"]
lists = [["111", "450", "nice"], ["can", "be", "of", "different", "sizes"]]
我想将第一个列表中的一项与列表中 n 个列表中的一项合并,示例:
对于 n = 1:
hello111
hello450
hellonice
hellocan
hellobe
...
或 n = 2
hello111can
hello111be
hello111of
...
n = 3 在这种情况下是不可能的 我在 python 中尝试使用 itertools 使用产品或其他东西,但我似乎无法理解如何做到这一点
[编辑] 我标记为正确的答案是我想要的,但使用排列而不是组合,非常感谢!
首先,使用 itertools.combinations(lists, n)
从 lists
中获取 n
个元素的组合,然后使用 itertools.product(words, *comb)
获取原始单词与该组合中的元素的乘积.您可以将这两个步骤合并为一个 double-loop 列表理解:
>>> n = 1
>>> [x for comb in itertools.combinations(lists, n) for x in itertools.product(words, *comb)]
[('hello', '111'),
('hello', '450'),
('hello', 'nice'),
('goodbye', '111'),
...
('foo', 'sizes')]
或 n = 2
:
[('hello', '111', 'can'),
('hello', '111', 'be'),
('hello', '111', 'of'),
('hello', '111', 'different'),
('hello', '111', 'sizes'),
('hello', '450', 'can'),
...
('foo', 'nice', 'sizes')]
对于 n = 3
及以上,您将获得 []
。
最后,只是 ''.join
那些在一起。 (我没有这样做,所以它更具可读性。)
>>> [''.join(x) for comb in itertools.combinations(lists, n) for x in itertools.product(words, *comb)]
from itertools import combinations, product
words = ["hello", "goodbye", "foo"]
lists = [["111", "450", "nice"], ["can", "be", "of", "different", "sizes"]]
# how many elements of `lists` to pick from?
for n in range(1, len(lists) + 1):
# This returns in-order combinations, ie you will get
# '111', 'can' and not 'can', '111'.
# If you want all orderings as well as all combinations,
# use itertools.permutations instead,
for sublist in combinations(lists, n):
# now we generate all combinations of
# one element from each basis list,
basis = [words] + list(sublist)
for combo in product(*basis):
# and display the result
print("".join(combo))
这给出了
hello111
hello450
hellonice
goodbye111
goodbye450
goodbyenice
foo111
foo450
foonice
hellocan
hellobe
helloof
hellodifferent
hellosizes
goodbyecan
goodbyebe
goodbyeof
goodbyedifferent
goodbyesizes
foocan
foobe
fooof
foodifferent
foosizes
hello111can
hello111be
hello111of
hello111different
hello111sizes
hello450can
hello450be
hello450of
hello450different
hello450sizes
hellonicecan
hellonicebe
helloniceof
hellonicedifferent
hellonicesizes
goodbye111can
goodbye111be
goodbye111of
goodbye111different
goodbye111sizes
goodbye450can
goodbye450be
goodbye450of
goodbye450different
goodbye450sizes
goodbyenicecan
goodbyenicebe
goodbyeniceof
goodbyenicedifferent
goodbyenicesizes
foo111can
foo111be
foo111of
foo111different
foo111sizes
foo450can
foo450be
foo450of
foo450different
foo450sizes
foonicecan
foonicebe
fooniceof
foonicedifferent
foonicesizes
在 n=2、n=3 等之前生成所有 n=1。如果您不关心顺序,则可以改为
for word in words:
combos = product(*([""] + sublist for sublist in lists))
next(combos) # skip n=0
for combo in combos:
print(word + "".join(combo))
产生
hellocan
hellobe
helloof
hellodifferent
hellosizes
hello111
hello111can
hello111be
hello111of
hello111different
hello111sizes
hello450
hello450can
hello450be
hello450of
hello450different
hello450sizes
hellonice
hellonicecan
hellonicebe
helloniceof
hellonicedifferent
hellonicesizes
goodbyecan
goodbyebe
goodbyeof
goodbyedifferent
goodbyesizes
goodbye111
goodbye111can
goodbye111be
goodbye111of
goodbye111different
goodbye111sizes
goodbye450
goodbye450can
goodbye450be
goodbye450of
goodbye450different
goodbye450sizes
goodbyenice
goodbyenicecan
goodbyenicebe
goodbyeniceof
goodbyenicedifferent
goodbyenicesizes
foocan
foobe
fooof
foodifferent
foosizes
foo111
foo111can
foo111be
foo111of
foo111different
foo111sizes
foo450
foo450can
foo450be
foo450of
foo450different
foo450sizes
foonice
foonicecan
foonicebe
fooniceof
foonicedifferent
foonicesizes
(相同列表,不同顺序)。