2016-01-05 51 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

相關問題