Python 3.x通过列表中的公共元素动态地将2d列表中的元素分组

时间:2014-03-13 21:49:47

标签: python list python-3.x grouping sublist

[('20bgx6', 'HQ', 'Head Quarters'),
('2040bl', 'NVA', 'North Vietnamese Army '),
 ('2040bl', 'HEAT', 'High Explosive Anti Tank '),
 ('2040bl', 'HEAT', 'High Explosive Anti Tank '),
 ('202kon', 'HEAT', 'High Explosive Anti Tank '),
 ('202kon', 'EFP', 'Explosively Formed Penetrator/Projectile'),
 ('202kon', 'NCO', 'Non-commissioned officer'),
 ('202kon', 'MRAP', 'Mine Resistant Ambush Protected'),
 ('202kon', 'UXO', 'Unexploded Ordnance'),
 ('202kon', 'MP', 'Military Police'),
 ('202kon', 'CQ', 'Charge of Quarters'),
 ('202kon', 'HQ', 'Head Quarters'),
 ('202kon', 'NCO', 'Non-commissioned officer'),
 ('1zz081', 'BC', 'Battalion Commander'),
 ('1zz081', 'HMMWV', 'High Mobility Multi Wheeled Vehicle'),
 ('1zz081', 'SALUTE', 'Size Activity Location Uniform Time Equipment '),
 ('1zxpbs', 'NCO', 'Non-commissioned officer'),
 ('1ztrv6', 'SALUTE', 'Size Activity Location Uniform Time Equipment '),
 ('1ztrv6', 'HEAT', 'High Explosive Anti Tank '),
 ('1ztrv6', 'BDU', 'Battle Dress Uniform, predecessor to the ACU'),
 ('1zs9gz', 'AG', 'Adjutant General'),
 ('1zs9gz', 'HEAT', 'High Explosive Anti Tank ')]

我将有一个与此相同的列表,然后我需要进行分组,以便所有以相同ID标签(2040bl,202kon等)开头的元素组将在他们自己的子列表中。< / p>

因此,以'2040bl'开头的子列表将在他们自己的组中结束,所有这些都在同一个列表中。结果列表的结果长度为7,因为有7个唯一的ID标记(每个子列表中的第一个元素)

我正在使用python 3

1 个答案:

答案 0 :(得分:4)

您可以使用itertools.groupby方法。根据您的数据,它似乎可以开箱即用。例如:

from itertools import groupby
for k, v in groupby(l, key=lambda t: t[0])  # assuming your list is stored in l
    print('%s\n\t%s' % (k, list(v)))

将输出:

20bgx6
        [('20bgx6', 'HQ', 'Head Quarters')]
2040bl
        [('2040bl', 'NVA', 'North Vietnamese Army '), ('2040bl', 'HEAT', 'High Explosive Anti Tank '), ('2040bl', 'HEAT', 'High Explosive Anti Tank ')]
202kon
        [('202kon', 'HEAT', 'High Explosive Anti Tank '), ('202kon', 'EFP', 'Explosively Formed Penetrator/Projectile'), ('202kon', 'NCO', 'Non-commissioned officer'), ('202kon', '
MRAP', 'Mine Resistant Ambush Protected'), ('202kon', 'UXO', 'Unexploded Ordnance'), ('202kon', 'MP', 'Military Police'), ('202kon', 'CQ', 'Charge of Quarters'), ('202kon', 'HQ', '
Head Quarters'), ('202kon', 'NCO', 'Non-commissioned officer')]
1zz081
        [('1zz081', 'BC', 'Battalion Commander'), ('1zz081', 'HMMWV', 'High Mobility Multi Wheeled Vehicle'), ('1zz081', 'SALUTE', 'Size Activity Location Uniform Time Equipment ')
]
1zxpbs
        [('1zxpbs', 'NCO', 'Non-commissioned officer')]
1ztrv6
        [('1ztrv6', 'SALUTE', 'Size Activity Location Uniform Time Equipment '), ('1ztrv6', 'HEAT', 'High Explosive Anti Tank '), ('1ztrv6', 'BDU', 'Battle Dress Uniform, predecess
or to the ACU')]
1zs9gz
        [('1zs9gz', 'AG', 'Adjutant General'), ('1zs9gz', 'HEAT', 'High Explosive Anti Tank ')]

重要提示:使用groupby时,需要按组密钥对数据进行排序,在您的示例中,它们是组密钥。否则,您可以在分组之前添加sorted(l, key=lambda t:t[0])