我创建了一个函数,用于统计一个国家出现在字典中的次数,并返回出现频率最高的国家/地区。如果多于一个国家出现的话,那么它应该返回一个国家名单。无法统计字典中的国家
例词典:
{'Leonardo da Vinci': [("Portrait of Isabella d'Este", 1499, 63.0, 46.0, 'chalk', 'France'),
('The Last Supper', 1495, 460.0, 880.0, 'tempera', 'Italy')],
'Pablo Picasso': [('Guernica', 1937, 349.0, 776.0, 'oil paint', 'Spain')]}
自法国,意大利和西班牙都只能在这本字典中出现一次函数应该返回
countries_appeared_most(dictionary1())
['France', 'Italy', 'Spain']
如果这些国家的一人,而不是出现2或3次功能将返回该国家。
我现在的代码下面我认为是接近解决方案,但我一直得到一个NameError匹配未定义。有谁知道我做错了什么?感谢您的帮助
代码:
def countries_appeared_most(db):
matches = {}
for painting_list in db.values():
for painting in painting_list:
country = painting[-1]
matches[country] = matches.get(country, 0) + 1
maxcount = max(matches.values())
themax = [k for k, count in matches.items() if count == maxcount]
[为什么你问同样的问题两次?](http://stackoverflow.com/questions/40599222/python-counting-countries-in-dictionary) –
这是一个不同的问题,我遇到了新问题代码,所以我问什么是错的 – warrior4223