我有一个包含A,B,C,D列的列表,我希望通过一组Unique D列的值重新格式化列表。 列表
预期产出:
list=[d1[[1,11,n1],[1,22,n2]],d2[[3,33,n3]]
然后打印d1下的所有列表:
[1,11,n1],[1,22,n2]
答案 0 :(得分:1)
您可以使用operator.itemgetter()提取感兴趣的字段(字段 3 是 D 列)。然后使用itertools.groupby()使用公共密钥提取组:
>>> from operator import itemgetter
>>> from itertools import groupby
>>> data = [ [1, 11,'n1','d1'],
[2, 22, 'n2', 'd1'],
[3, 33, 'n3', 'd2'],
]
>>> data.sort(key=itemgetter(3)) # sort by column d
>>> result = []
>>> for k, groups in groupby(data, key=itemgetter(3)):
entry = [group[:-1] for group in groups]
result.append((k, entry))
>>> result
[('d1', [[1, 11, 'n1'], [2, 22, 'n2']]), ('d2', [[3, 33, 'n3']])]
如果您只想打印d1
组,则代码更简单:
>>> for k, groups in groupby(data, key=itemgetter(3)):
if k == 'd1':
print([group[:-1] for group in groups])
[[1, 11, 'n1'], [2, 22, 'n2']]
代码[group[:-1] for group in groups]
使用slicing和list comprehension删除关键字段,如预期输出所示。
希望这会有所帮助: - )