解压缩元组列表

时间:2016-05-31 20:16:43

标签: python list tuples

如何解压缩以下列表

[('1', 'GENERAL', '1'), ('1.1', 'RELATED DOCUMENTS', '1'), ('1.2', 'SUMMARY', '1'), ('1.3', 'DEFINITIONS', '1'), ('1.4', 'INFORMATIONAL SUBMITTALS', '2'), ('1.5', 'GENERAL COORDINATION PROCEDURES', '2'), ('1.6', 'COORDINATION DRAWINGS', '3'), ('1.7', 'REQUESTS FOR INFORMATION (RFIs)', '4'), ('1.8', 'PROJECT MEETINGS', '6')]
[[('2', 'PRODUCTS – NOT APPLICABLE', '10')]]

从我试过的其他帖子的解决方案。

Part, Title, Page = zip(*text_good[0])

但得到了错误

 too many values to unpack (expected 3)

我也试过

Part1[a].append(Part for Part, Title, Page in text_good[0])
Part2[a].append(Part for Part, Title, Page in text_good[1])
Part3[a].append(Part for Part, Title, Page in text_good[2])

但是这似乎在内存中返回了一个位置,我无法打开数组,因为我收到一个错误,指出它不可选。

由于

更新: text_good的分配

for i in range(0, len(text_between_parts)):
    text_good[i].append(re.findall(r'\s*(\b\d+(?:[.]\d+)?)\W+\s*(.*?)\s*(\b\d+\b)', text_between_parts[i]))

更新2:当我做text_good [0]时,我得到了

[[('1', 'GENERAL', '1'), ('1.1', 'RELATED DOCUMENTS', '1'), ('1.2', 'SUMMARY', '1'), ('1.3', 'DEFINITIONS', '1'), ('1.4', 'INFORMATIONAL SUBMITTALS', '2'), ('1.5', 'GENERAL COORDINATION PROCEDURES', '2'), ('1.6', 'COORDINATION DRAWINGS', '3'), ('1.7', 'REQUESTS FOR INFORMATION (RFIs)', '4'), ('1.8', 'PROJECT MEETINGS', '6')]]

当我做text_good [0] [0]时,我得到了

[('1', 'GENERAL', '1'), ('1.1', 'RELATED DOCUMENTS', '1'), ('1.2', 'SUMMARY', '1'), ('1.3', 'DEFINITIONS', '1'), ('1.4', 'INFORMATIONAL SUBMITTALS', '2'), ('1.5', 'GENERAL COORDINATION PROCEDURES', '2'), ('1.6', 'COORDINATION DRAWINGS', '3'), ('1.7', 'REQUESTS FOR INFORMATION (RFIs)', '4'), ('1.8', 'PROJECT MEETINGS', '6')]

当我执行text_good [0]时,请注意额外的括号。

1 个答案:

答案 0 :(得分:3)

好的,我认为我们首先需要做一点澄清。我对这个列表究竟是什么感到困惑所以我会做出以下假设(如果这些假设中的任何一个是错误的,请告诉我,以便我可以修复它们):

text_good = [[('1', 'GENERAL', '1'), ('1.1', 'RELATED DOCUMENTS', '1'), ('1.2', 'SUMMARY', '1'), ('1.3', 'DEFINITIONS', '1'), ('1.4', 'INFORMATIONAL SUBMITTALS', '2'), ('1.5', 'GENERAL COORDINATION PROCEDURES', '2'), ('1.6', 'COORDINATION DRAWINGS', '3'), ('1.7', 'REQUESTS FOR INFORMATION (RFIs)', '4'), ('1.8', 'PROJECT MEETINGS', '6')], [('2', 'PRODUCTS - NOT APPLICABLE', '10')]]

如果我text_good[0]我现在在哪里:

[('1', 'GENERAL', '1'),
 ('1.1', 'RELATED DOCUMENTS', '1'),
 ('1.2', 'SUMMARY', '1'),
 ('1.3', 'DEFINITIONS', '1'),
 ('1.4', 'INFORMATIONAL SUBMITTALS', '2'),
 ('1.5', 'GENERAL COORDINATION PROCEDURES', '2'),
 ('1.6', 'COORDINATION DRAWINGS', '3'),
 ('1.7', 'REQUESTS FOR INFORMATION (RFIs)', '4'),
 ('1.8', 'PROJECT MEETINGS', '6')]

text_good[1]将是:

[('2', 'PRODUCTS - NOT APPLICABLE', '10')]   

对我来说,这似乎有一个元组列表,其中('1', 'GENERAL', '1')将按顺序对应Part, Title, Page

如果是这种情况,你需要做这样的事情:

Parts, Title, Page = zip(*[t for l in text_good for t in l])

在这种情况下你会得到:

print Parts # ('1', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '1.7', '1.8', '2')
print Title # ('GENERAL',
            # 'RELATED DOCUMENTS',
            # 'SUMMARY',
            # 'DEFINITIONS',
            # 'INFORMATIONAL SUBMITTALS',
            # 'GENERAL COORDINATION PROCEDURES',
            # 'COORDINATION DRAWINGS',
            # 'REQUESTS FOR INFORMATION (RFIs)',
            # 'PROJECT MEETINGS',
            # 'PRODUCTS - NOT APPLICABLE')

print Page # ('1', '1', '1', '1', '2', '2', '3', '4', '6', '10')

最终编辑: 因为@JStuff有list of lists of lists of tuples,所以从技术上讲,我们需要3个for循环才能提取他想要的定义。

Parts, Title, Page = [t for l in text_good for ll in l for t in ll] # Yay for list comprehension?