Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 根据python列表中的某些条件查找最小值?_Python 3.x_List_Dictionary - Fatal编程技术网

Python 3.x 根据python列表中的某些条件查找最小值?

Python 3.x 根据python列表中的某些条件查找最小值?,python-3.x,list,dictionary,Python 3.x,List,Dictionary,我有一个列表值。我想找到一个基于相同类别值的秩的最小值 [{'PlayerName': 'aaaa', 'Category': 'MVP', 'Rank': 1}, {'PlayerName': ' bbbb', 'Category': 'MVP', 'Rank': 18}, {'PlayerName': ' cccc', 'Category': 'LVP', 'Rank': 2}, {'PlayerName': ' ddddd', 'Category': 'LVP', 'Rank': 33}]

我有一个列表值。我想找到一个基于相同类别值的秩的最小值

[{'PlayerName': 'aaaa', 'Category': 'MVP', 'Rank': 1}, {'PlayerName': ' bbbb', 'Category': 'MVP', 'Rank': 18}, {'PlayerName': ' cccc', 'Category': 'LVP', 'Rank': 2}, {'PlayerName': ' ddddd', 'Category': 'LVP', 'Rank': 33}]

在列表上方,输入值以获得如下输出

[{'PlayerName': 'aaaa', 'Category': 'MVP', 'Rank': 1},{'PlayerName': ' cccc', 'Category': 'LVP', 'Rank': 2}]

非常感谢您的建议

这是一种使用
dict.setdefault
到groupby
Category
的方法,并且是一个简单的迭代

Ex:

data = [{'PlayerName': 'aaaa', 'Category': 'MVP', 'Rank': 1}, {'PlayerName': ' bbbb', 'Category': 'MVP', 'Rank': 18}, {'PlayerName': ' cccc', 'Category': 'LVP', 'Rank': 2}, {'PlayerName': ' ddddd', 'Category': 'LVP', 'Rank': 33}]
r = {}
for i in data:
    r.setdefault(i['Category'], []).append(i)
print([min(v, key=lambda x: x['Rank']) for _, v in r.items()])
[{'PlayerName': 'aaaa', 'Category': 'MVP', 'Rank': 1}, {'PlayerName': ' cccc', 'Category': 'LVP', 'Rank': 2}]
输出:

data = [{'PlayerName': 'aaaa', 'Category': 'MVP', 'Rank': 1}, {'PlayerName': ' bbbb', 'Category': 'MVP', 'Rank': 18}, {'PlayerName': ' cccc', 'Category': 'LVP', 'Rank': 2}, {'PlayerName': ' ddddd', 'Category': 'LVP', 'Rank': 33}]
r = {}
for i in data:
    r.setdefault(i['Category'], []).append(i)
print([min(v, key=lambda x: x['Rank']) for _, v in r.items()])
[{'PlayerName': 'aaaa', 'Category': 'MVP', 'Rank': 1}, {'PlayerName': ' cccc', 'Category': 'LVP', 'Rank': 2}]