我正在尝试使用以下词典(' ContinentDict ')按大陆对国家/地区进行分类。
因此,我想按值对键进行分类。
ContinentDict = {'China':'Asia',
'United States':'North America',
'Japan':'Asia',
'United Kingdom':'Europe',
'Russian Federation':'Europe',
'Canada':'North America',
'Germany':'Europe',
'India':'Asia',
'France':'Europe',
'South Korea':'Asia',
'Italy':'Europe',
'Spain':'Europe',
'Iran':'Asia',
'Australia':'Australia',
'Brazil':'South America'}
当我尝试选项 1 时:
v = {}
for key, value in sorted(d.items()):
v.setdefault(value, []).append(key)
我收到错误:
Traceback (most recent call last):
File "<input>", line 2, in <module>
TypeError:'dict' object is not callable
当我尝试选项 2 时:
from collections import defaultdict
dictionary = defaultdict(list)
for key, value in ContinentDict:
dictionary[value].append(key)
我收到错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: first argument must be callable or None
有人可以帮我吗?
动漫人物
相关分类