Skip to main content
 首页 » 编程设计

python之创建包含成对的另一个列表的列表

2025年05月04日14jirigala

很抱歉我无法以优雅的方式写出问题。我希望进一步的文字足以理解我的问题。

我有一组列表,其中包含一到五个成员。我想从每个列表创建图表。

我的数据:

data = [ 
['07F', '05F', '10F', '06F'], 
['05T', '05F', '02T'], 
['03T', '03F'] 
] 

我希望每个列表都会产生所有可能的列表对:

['07F', '05F', '10F', '06F'] should become -----------> 
 
['07F',05F'] 
['07F',10F'] 
['07F',06F'] 
['05F',10F'] 
['05F',06F'] 
['10F',06F'] 

与所有下一个列表相同。

我所理解的是我必须遍历列表,然后..?

请您参考如下方法:

import itertools 
from pprint import pprint 
data = [ 
    ['07F', '05F', '10F', '06F'], 
    ['05T', '05F', '02T'], 
    ['03T', '03F'] 
    ]  
 
# Create a list of the lists of pair-wise combinations 
data = [list(itertools.combinations(x,2)) for x in data] 
 
pprint(data)