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

时间:2016-01-05 15:35:13

标签: python list combinations product itertools

我将尝试用一个例子来解释这一点,因为我似乎有问题向自己解释:

想象一下,我有一个字符串列表,以及另一个字符串列表列表:

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

我想将第一个列表中的1个项目与列表中的n个列表中的1个项目组合起来,例如:

对于n = 1:

hello111
hello450
hellonice
hellocan
hellobe
...

或n = 2

hello111can
hello111be
hello111of
...
在这种情况下,

n = 3是不可能的 我在python中使用itertools使用产品或其他东西尝试这个但是我似乎无法理解如何做到这一点

[编辑] 我标记为正确的答案是我想要的,但是使用排列而不是组合,谢谢TON!

2 个答案:

答案 0 :(得分:1)

首先,使用function loadUrls() { var myurl=new Array(); for(var i=0;i<5;i++) { myurl[i] = document.getElementById("urls").value.split('\n'); window.open(myurl[i]); } } 获取n lists元素的组合,然后使用itertools.combinations(lists, n)获取原始单词和该组合中元素的乘积。您可以将两个步骤组合成一个双循环列表理解:

itertools.product(words, *comb)

>>> 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

答案 1 :(得分:1)

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

(相同的清单,不同的顺序)。

相关问题