有没有办法将字典作为另一个字典的键使用?目前我是用两个列表来实现,但使用字典会更好。以下是我当前的实现方式:
dicts = [{1:'a', 2:'b'}, {1:'b', 2:'a'}]
corresponding_name = ['normal', 'switcheroo']
if {1:'a', 2:'b'} in dicts:
dict_loc = dicts.index({1:'a', 2:'b'})
desired_name = corresponding_name[dict_loc]
print desired_name
我的要求如下:
dict_dict = {{1:'a', 2:'b'}:'normal', {1:'b', 2:'a'}:'switcheroo'}
try: print dict_dict[{1:'a', 2:'b'}]
except: print "Doesn't exist"
但是这并不起作用,我不确定是否有任何方法可以解决这个问题。