どのようなソリューションを使用する場合でも、それを堅牢にしたい場合は、おそらく、一方を更新するときに他方の方向を自動的に更新するクラスをラップする必要があります。たとえば、@mgilson の手法を使用した基本的な双方向辞書の開始は次のとおりです (つまり、相互にマッピングしている 2 つのアイテム セットの間に重複がある場合は機能しません。ただし、異なる型を持つことはうまく機能します)。
class BiDict(dict):
"""Bidirectional Dictionary - setting 'key' to 'value' also
sets 'value' to 'key' (so don't use overlapping mappings)
"""
def __init__(self, *args):
super(BiDict, self).__init__(*args)
# After regular dict initialization, loop over any items
# and add their reverse. Note that we can't use any of the
# iter* methods here since we're adding items in the body
# of the loop.
for key in self.keys():
super(BiDict, self).__setitem__(self[key], key);
def __setitem__(self, key, val):
# If the key has an old value, delete its reverse
if key in self:
super(BiDict, self).__delitem__(self[key])
# Then add both forward and reverse for the new value
super(BiDict, self).__setitem__(key, val);
super(BiDict, self).__setitem__(val, key);
def __delitem__(self, key):
# delete both directions
if key in self:
super(BiDict, self).__delitem__(self[key]);
super(BiDict, self).__delitem__(key);
次のように使用できます。
>>> from bidict import BiDict
>>> d = BiDict({'a':1,'b':2})
>>> d['a']
1
>>> d[2]
'b'
>>> d['c']=3
>>> d[3]
'c'
>>> del d['a']
>>> d['a']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'
>>> d[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1