在Python中对dict中的值进行分组和排序

在Python中对dict中的值进行分组和排序,python,sorting,group-by,Python,Sorting,Group By,我有一个python字典,如下所示,我必须按“标签”分组,并获得每个“标签”的最高“置信度”值 [{'label': 'id', 'confidence': 0.11110526, 'topleft': {'x': 0, 'y': 0}, 'bottomright': {'x': 187, 'y': 57}}, {'label': 'id', 'confidence': 0.10690566, 'topleft': {'x': 265, 'y': 0}, 'bottom

我有一个python字典,如下所示,我必须按“标签”分组,并获得每个“标签”的最高“置信度”值

[{'label': 'id',
  'confidence': 0.11110526,
  'topleft': {'x': 0, 'y': 0},
  'bottomright': {'x': 187, 'y': 57}},

{'label': 'id',
  'confidence': 0.10690566,
  'topleft': {'x': 265, 'y': 0},
  'bottomright': {'x': 525, 'y': 54}},

 {'label': 'name',
  'confidence': 0.15541315,
  'topleft': {'x': 9, 'y': 24},
  'bottomright': {'x': 116, 'y': 58}},

 {'label': 'group',
  'confidence': 0.12578075,
  'topleft': {'x': 53, 'y': 24},
  'bottomright': {'x': 153, 'y': 61}},

 {'label': 'name',
  'confidence': 0.12709439,
  'topleft': {'x': 0, 'y': 0},
  'bottomright': {'x': 247, 'y': 84}},

 {'label': 'group',
  'confidence': 0.116156094,
  'topleft': {'x': 96, 'y': 23},
  'bottomright': {'x': 191, 'y': 61}}]    

如何有效地实现这一点

您可以使用
groupby

for n,g in groupby(tst,key=lambda x:x['label']):
     print n,max(list(g),key=lambda x:x['confidence']).get('confidence')
结果:

id 0.11110526
name 0.15541315
group 0.12578075
name 0.12709439
group 0.116156094