【发布时间】:2016-11-14 21:59:50
【问题描述】:
我正在开发一个功能,我需要在字典中找到具有最多值的键(艺术家姓名)。有时两个键具有相同数量的值,在这种情况下,我需要返回艺术家姓名的列表。
词典示例:
{'M':[("One",1400,30.0, 20.5,"oil paint","Austria"),("Three",1430,100.0,102.0,"watercolor","France")],
'P':[("Eight",1460, 225.0, 200.0, "fresco","Netherlands")],
'U':[("Nine",1203,182.0, 957.0,"egg tempera","Italy"), ("Twelve",1200,76.2,101.6,"egg tempera","France")]
}
对于这个字典,因为 M 和 U 的值最多(M 有 2,U 有 2,而 P 只有 1)函数应该返回
artists_with_most_work(dictionary1())
['M', 'U']
如何搜索每个键的值的数量并返回具有最多的值?我认为使用 max() 会是一个好主意,但我认为我在下面的当前尝试中没有正确使用它。感谢任何可以提供帮助的人
代码:
def artist_with_most_work(db):
matches = []
for key, record_list in db.items():
for record in record_list:
if item in record:
max(db) = themax
matches.append(themax)
return matches
【问题讨论】:
标签: python python-3.x find key max