0

我有一个清单

[
 [[[u'ANC'], [u'DEN', u'SFO'], [u'CHI', u'CLE', u'DEN', u'EWR', u'HOU', u'WAS'], [u'GSP']]], 
 [[[u'ANC'], [u'PDX', u'SEA'], [u'CHI', u'CLE', u'DEN', u'EWR', u'HOU', u'LAX', u'SFO', u'WAS'], [u'GSP']]]
]

列表已从此文本创建

ANC-DEN/SFO-CHI/CLE/DEN/EWR/HOU/WAS-GSP
ANC-PDX/SEA-CHI/CLE/DEN/EWR/HOU/LAX/SFO/WAS-GSP

我需要获取可以使用该列表完成的所有变体:

ANC-SFO-EWR-GSP
ANC-SFO-CHI-GSP
ANC-SFO-CLE-GSP
ANC-DEN-EWR-GSP
ANC-PDX-EWR-GSP
ANC-SFO-HOU-GSP
ANC-SEA-LAX-GSP
ANC-SEA-EWR-GSP
ANC-SFO-ORD-GSP
ANC-PDX-LAX-GSP
ANC-PDX-CHI-GSP
ANC-DEN-CHI-GSP
ANC-SEA-SFO-GSP
ANC-PDX-SFO-GSP
ANC-SEA-CHI-GSP
ANC-SFO-DEN-GSP
ANC-PDX-HOU-GSP
ANC-SEA-HOU-GSP
ANC-DEN-HOU-GSP
ANC-DEN-CLE-GSP
ANC-PDX-CLE-GSP
ANC-SEA-CLE-GSP
ANC-DEN-ORD-GSP
ANC-PDX-ORD-GSP
ANC-PDX-DEN-GSP
ANC-SEA-DEN-GSP
ANC-SEA-ORD-GSP

我该怎么做呢?

4

1 回答 1

7

使用itertools.product列表并将其扩展为参数:

>>> import itertools
>>> test = [['a'], ['b', 'c'], ['d']]
>>> list(itertools.product(*test))
    [('a', 'b', 'd'), ('a', 'c', 'd')]

在你的情况下,它会是这样的:

for row in data:
    for item in itertools.product(*row[0]):
        print '-'.join(item)
于 2013-02-21T01:27:45.820 回答