我有一個列表清單,沒有。內部列表的是未知:如何排列/組合不同大小的列表? python
>>> x = [1,2,3]
>>> y = [4,5,6,7]
>>> z = [8]
>>> lol = [x,y,z]
我需要在lol
列表的順序內部列表內得到每個項目的組合,我一直在做它是這樣來獲得所需的輸出:
>>> for i in x:
... for j in y:
... for k in z:
... print [i,j,k]
...
[1, 4, 8]
[1, 5, 8]
[1, 6, 8]
[1, 7, 8]
[2, 4, 8]
[2, 5, 8]
[2, 6, 8]
[2, 7, 8]
[3, 4, 8]
[3, 5, 8]
[3, 6, 8]
[3, 7, 8]
什麼是pythonic方式做以上?這是否有itertools
函數?
我已經試過itertools.product
但我沒有得到期望的輸出:
>>> from itertools import product
>>> for i in product(lol):
... print i
...
([1, 2, 3],)
([4, 5, 6, 7],)
([8],)
您的意思是'產品(*大聲笑)'? – jonrsharpe 2014-10-20 15:37:57