簡體   English   中英

如何找到列表列表的所有可能組合(在Python中)?

[英]How can I find all the possible combinations of a list of lists (in Python)?

我在Python中有以下結構:

letters = [['a', 'b', 'c'], ['p', 'q', 'r', 's'], ['j', 'k', 'l']]

我想按照它們當前存在的順序找到所有可能的字母組合。 對於上面的例子,這將是:

apj
apk
apl
aqj
aqk
aql
...
csk
csl

這似乎應該是一件非常簡單的事情,但我無法弄明白。

在Python 2.6或更高版本中,您可以使用itertools.product

>>> import itertools
>>> map(''.join, itertools.product(*letters))
apj
apk
apl
aqj
aqk
aql
...etc...
csk
csl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM