辞書を辞書のキーとして使用する方法はありますか?現在、私はこれに対して2つのリストを使用していますが、辞書を使用するのが良いでしょう。ここで私は現在やっているものです:辞書を辞書のキーとして使用する
ここ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"
をしかし、これは動作しませんし、私はこの周りにどのような方法があるかどうかわかりません。
あなたは辞書のキーとして可変オブジェクトを使用するので、ありませんすることはできません。それは不可能。 – zondo
それを行うことはできませんが、[名前付きタプル](http://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python)が良い代替手段になる可能性があります。 – JGreenwell