2016-01-05 53 views
3

我要去尝试用一个例子来解释这一点,因为我似乎有它解释给自己一个问题:希望列表中的每个元素有n的每一个元素结合列出

想象我有一个字符串列表和字符串列表的另一个列表:

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

我想第一个列表的项目1项,只有1项列表ñ列表,例如结合:

n = 1时:

hello111 
hello450 
hellonice 
hellocan 
hellobe 
... 

或n = 2

hello111can 
hello111be 
hello111of 
... 

N = 3将不会在这种情况下有可能 我使用的产品或东西itertools尝试这种在python,但我似乎无法绕到我的头如何做到这一点

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

+0

我想我理解'n == 1'的例子,但是我不明白'n == 2'的情况应该如何工作。 – timgeb

+1

对于一般情况,请列举n = 2和三个子列表的示例! –

+0

对于n = 2时,是否希望hello11​​1can和hellocan111,或者只是第一个? – FCo

回答

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 = 1,N = 2之前,N = 3,等等。如果你不这样做关心的排序,你可以改为做

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 

(相同的列表,不同的顺序)。

+0

你不能使用功能以外的产量 – heinst

+0

好男人;)正是我所需要的,但是正如你所提及的排列组合!你们怎么把你的头围绕着这个,该死的!非常好的工作,谢谢 – Esser420

1

首先,使用从itertools.combinations(lists, n)得到listsn元件的组合,然后得到的原话产品,然后使用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那些在一起。 (我没那么它更具有可读性。)

>>> [''.join(x) for comb in itertools.combinations(lists, n) for x in itertools.product(words, *comb)] 
+0

他想要一个字符串列表。 “x”应该用“”替换。加入(x)'给OP想要的东西 – heinst

+0

@heinst是的,这正是我在最后一行中所说的。不过,我认为这些清单会让这些组合更加明确。 –

+0

啊没看到!抱歉! – heinst

相关问题