Python 从列表中获取具有最大值的子列表
我试图检索列表中具有最大键值的子列表 在本例中,我试图检索具有最大置信度得分的子列表:Python 从列表中获取具有最大值的子列表,python,list,max,sublist,Python,List,Max,Sublist,我试图检索列表中具有最大键值的子列表 在本例中,我试图检索具有最大置信度得分的子列表: listA=[{'id': '1', 'actions': [{'acc': 'ac1', 'coordinates': [569, 617, 627, 631], 'confidence': 93.0}]}, {'id': '1', 'actions': [{'acc': 'acc1','coordinates': [569, 617, 627, 631], 'confidence': 94.0}]},
listA=[{'id': '1', 'actions': [{'acc': 'ac1', 'coordinates': [569, 617, 627, 631], 'confidence': 93.0}]}, {'id': '1', 'actions': [{'acc': 'acc1','coordinates': [569, 617, 627, 631], 'confidence': 94.0}]}, {'id': '1', 'actions': [{'acc': 'acc1', 'coordinates': [569, 617, 627, 631], 'confidence': 95.0}]}]
预期产出为:
[{'id': '1', 'actions': [{'acc': 'acc1', 'coordinates': [569, 617, 627, 631], 'confidence': 95.0}]
我使用了itemgetter,但它没有返回所需的输出。我使用了python的max函数,并为它提供了一个键,该键将使用置信键值作为查找最大值的方法
listA=[{'id': '1', 'actions': [{'acc': 'ac1', 'coordinates': [569, 617, 627, 631], 'confidence': 93.0}]},
{'id': '1', 'actions': [{'acc': 'acc1','coordinates': [569, 617, 627, 631], 'confidence': 94.0}]},
{'id': '1', 'actions': [{'acc': 'acc1', 'coordinates': [569, 617, 627, 631], 'confidence': 95.0}]}]
maxList = max(listA, key=lambda x: x['actions'][0]['confidence'])
print(maxList)
如果希望返回项目的排序列表,而不仅仅是最大值,则可以执行几乎完全相同的操作。您只需将max
替换为sorted
编辑:谢谢@tobias_k的建议。如果有多个操作,请将lambda替换为
lambda x:max(对于x中的a['actions'])
我使用了python的max函数,并为其提供了一个键,该键将使用置信键值作为查找最大值的方法
listA=[{'id': '1', 'actions': [{'acc': 'ac1', 'coordinates': [569, 617, 627, 631], 'confidence': 93.0}]},
{'id': '1', 'actions': [{'acc': 'acc1','coordinates': [569, 617, 627, 631], 'confidence': 94.0}]},
{'id': '1', 'actions': [{'acc': 'acc1', 'coordinates': [569, 617, 627, 631], 'confidence': 95.0}]}]
maxList = max(listA, key=lambda x: x['actions'][0]['confidence'])
print(maxList)
如果希望返回项目的排序列表,而不仅仅是最大值,则可以执行几乎完全相同的操作。您只需将max
替换为sorted
编辑:谢谢@tobias_k的建议。如果有多个操作,请将lambda替换为
lambda x:max(对于x中的a['actions'])
这里有一个可能的解决方案,您会发现lambda很难理解
我将其制作得非常冗长,并且对初学者非常友好,因此新来者也可以使用它:
listA=[{'id': '1', 'actions': [{'acc': 'ac1', 'coordinates': [569, 617, 627, 631], 'confidence': 93.0}]}, {'id': '1', 'actions': [{'acc': 'acc1','coordinates': [569, 617, 627, 631], 'confidence': 94.0}]}, {'id': '1', 'actions': [{'acc': 'acc1', 'coordinates': [569, 617, 627, 631], 'confidence': 95.0}]}]
max_confidence_item = None
max_confidence_value = float('-inf') # This is just a VERY small number so that we are sure that every confidence we find as first is bigger than this number
for item in listA:
current_confidence_value = item['actions'][0]['confidence']
current_confidence_item = item
if (max_confidence_value < current_confidence_value):
max_confidence_value = current_confidence_value
max_confidence_item = current_confidence_item
print(max_confidence_item)
这里有一个可能的解决办法,就是你发现lambda很难理解 我将其制作得非常冗长,并且对初学者非常友好,因此新来者也可以使用它:
listA=[{'id': '1', 'actions': [{'acc': 'ac1', 'coordinates': [569, 617, 627, 631], 'confidence': 93.0}]}, {'id': '1', 'actions': [{'acc': 'acc1','coordinates': [569, 617, 627, 631], 'confidence': 94.0}]}, {'id': '1', 'actions': [{'acc': 'acc1', 'coordinates': [569, 617, 627, 631], 'confidence': 95.0}]}]
max_confidence_item = None
max_confidence_value = float('-inf') # This is just a VERY small number so that we are sure that every confidence we find as first is bigger than this number
for item in listA:
current_confidence_value = item['actions'][0]['confidence']
current_confidence_item = item
if (max_confidence_value < current_confidence_value):
max_confidence_value = current_confidence_value
max_confidence_item = current_confidence_item
print(max_confidence_item)
confidence=0
i=0
对于范围内的(len(listA)):
如果信心
置信度=0
i=0
对于范围内的(len(listA)):
如果信心
到目前为止您尝试了什么?我已经提到过的itemgetter。请检查。是否可以有多个操作
,然后它应该做什么?到目前为止您尝试了什么?我已经提到了itemgetter。请检查。是否可以有多个操作
,然后它应该做什么?啊,提前3分钟。我将删除我的答案,并投你的赞成票。那么,第一次,第一次。万岁!或者可能lambda x:max(a在x中['actions'])
!我不确定行动是否需要这个。啊,3分钟前。我将删除我的答案,并投你的赞成票。那么,第一次,第一次。万岁!或者可能lambda x:max(a在x中['actions'])
!我不确定OP是否需要这个。