简体   繁体   English

想要将列表的每个元素与n个列表的每个元素组合在一起

[英]want to combine every element of a list with every element of n lists

I'm going to try to explain this with an example, since I seem to have a problem explaining it to myself: 我将尝试用一个例子来解释这个,因为我似乎有一个问题向自己解释:

imagine I have a list of strings, and another list of lists of strings: 想象一下,我有一个字符串列表,以及另一个字符串列表列表:

words = ["hello", "goodbye", "foo"]
lists = [["111", "450", "nice"], ["can", "be", "of", "different", "sizes"]]

I want to combine 1 item of the first list with only 1 item of n lists in lists, example: 我想将第一个列表中的1个项目与列表中的n个列表中的1个项目组合,例如:

For n = 1: 对于n = 1:

hello111
hello450
hellonice
hellocan
hellobe
...

or n = 2 或者n = 2

hello111can
hello111be
hello111of
...

n = 3 would not be possible in this scenario I'm trying this in python with itertools using product or something but I can't seem to wrap my head around how to do this n = 3在这种情况下是不可能的我在python中使用itertools使用产品或其他东西尝试这个但我似乎无法绕过如何做到这一点

[EDIT] The answer I marked as correct is what I want but with permutations instead of combinations, Thanks a TON! [编辑]我标记为正确的答案是我想要的,但使用排列而不是组合,谢谢TON!

First, get the combinations of n elements from lists using itertools.combinations(lists, n) , then get the product of the original words and the elements from that combination using itertools.product(words, *comb) . 首先,使用itertools.combinations(lists, n)lists获取n元素的组合,然后使用itertools.product(words, *comb)获取原始单词和该组合中的元素的乘积。 You can combine both steps into one double-loop list comprehension: 您可以将两个步骤组合成一个双循环列表理解:

>>> 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')]

Or for n = 2 : 或者对于n = 2

[('hello', '111', 'can'),
 ('hello', '111', 'be'),
 ('hello', '111', 'of'),
 ('hello', '111', 'different'),
 ('hello', '111', 'sizes'),
 ('hello', '450', 'can'),
 ...
 ('foo', 'nice', 'sizes')]

And for n = 3 and above you get [] . 对于n = 3及以上,你得到[]

Finally, just ''.join those together. 最后,只是''.join 。加在一起。 (I did not so it's more readable.) (我没有这样,它更具可读性。)

>>> [''.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))

which gives 这使

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

That produces all n=1 before n=2, n=3, etc. If you do not care about the ordering, you could instead do 在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))

which produces 哪个产生

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

(same list, different order). (相同的清单,不同的顺序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM