【发布时间】:2013-10-23 13:53:29
【问题描述】:
所以我有一个像这样的元组列表:
[
('Worksheet',),
('1a', 'Calculated'),
('None', 'None', 'None', 'None', 'None'),
('1b', 'General'),
('1b', 'General', 'Basic'),
('1b', 'General', 'Basic', 'Data'),
('1b', 'General', 'Basic', 'Data', 'Line 1'),
('1b', 'General', 'Basic', 'Data', 'Line 2'),
('None', 'None', 'None', 'None', 'None'),
('1c', 'General'),
('1c', 'General', 'Basic'),
('1c', 'General', 'Basic', 'Data'),
('None', 'None', 'None', 'None', 'None'),
('2', 'Active'),
('2', 'Active', 'Passive'),
('None', 'None', 'None', 'None', 'None'),
...
]
每个元组的长度为 1-5。我需要递归地减少列表以结束:
[
('Worksheet',),
('1a', 'Calculated'),
('None', 'None', 'None', 'None', 'None'),
('1b', 'General', 'Basic', 'Data', 'Line 1'),
('1b', 'General', 'Basic', 'Data', 'Line 2'),
('None', 'None', 'None', 'None', 'None'),
('1c', 'General', 'Basic', 'Data'),
('None', 'None', 'None', 'None', 'None'),
('2', 'Active', 'Passive'),
('None', 'None', 'None', 'None', 'None'),
...
]
基本上,如果下一行匹配上一行中的所有内容,+1 将其删除,直到具有相同层次结构的元组的最大长度。
因此,在我的示例中,有 3 行 1c 是元组中的第一项,因此它被缩减为最长。
【问题讨论】:
-
你有没有尝试过?你被困在哪里了?