I am trying to use the following dictionary ('ContinentDict') to bin countries by continent.
Thus, I would like to bin keys by value.
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'}
When I try option 1:
v = {}
for key, value in sorted(d.items()):
v.setdefault(value, []).append(key)
I get the error:
Traceback (most recent call last):
File "<input>", line 2, in <module>
TypeError:'dict' object is not callable
When I try option 2:
from collections import defaultdict
dictionary = defaultdict(list)
for key, value in ContinentDict:
dictionary[value].append(key)
I get the error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: first argument must be callable or None
Could anybody give me a helping hand?