对称字典,其中d [a] [b] == d [b] [a] [英] Symmetric dictionary where d[a][b] == d[b][a]

查看:91
本文介绍了对称字典,其中d [a] [b] == d [b] [a]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有一个算法,可为值对创建度量,其中 m(v1,v2)== m(v2,v1)(即对称) )。我的想法是写一个字典字典,这些值以一种内存有效的方式存储在其中,以便可以用任何顺序的键轻松地检索它们。我喜欢继承事物,理想情况下,我很想写一个 symmetric_dict 其中 s_d [v1] [v2] 始终等于 s_d [v2] [v1] ,可能是根据某种排序关系检查v中哪个较大,然后将它们切换到较小的那个元素一总是被首先提及。例如,当调用 s_d [5] [2] = 4 时,字典的dict将把它们转过来,以便它们实际上存储为 s_d [ 2] [5] = 4 ,对于数据检索而言也是如此。
我也非常乐于寻求更好的数据结构,但是我更喜欢使用 is-a关系实现而不是仅使用dict并预处理一些函数参数的实现。

I have an algorithm in python which creates measures for pairs of values, where m(v1, v2) == m(v2, v1) (i.e. it is symmetric). I had the idea to write a dictionary of dictionaries where these values are stored in a memory-efficient way, so that they can easily be retrieved with keys in any order. I like to inherit from things, and ideally, I'd love to write a symmetric_dict where s_d[v1][v2] always equals s_d[v2][v1], probably by checking which of the v's is larger according to some kind of ordering relation and then switching them around so that the smaller element one is always mentioned first. i.e. when calling s_d[5][2] = 4, the dict of dicts will turn them around so that they are in fact stored as s_d[2][5] = 4, and the same for retrieval of the data. I'm also very open for a better data structure, but I'd prefer an implementation with "is-a" relationship to something which just uses a dict and preprocesses some function arguments.

推荐答案

这是一种略有不同的方法,看起来很有希望。尽管 SymDict 类不是 dict 子类,但它的行为基本上像一个子类,并且只有一个私有字典参与。我认为一个有趣的功能是它保留了您似乎想要的自然 [] [] 查找语法的事实。

Here's a slightly different approach that looks promising. Although the SymDict class isn't a dict subclass, it mostly behaves like one, and there's only a single private dictionary involved. I think one interesting feature is that fact that it preserves the natural [][] lookup syntax you seemed to want.

class SymDict(object):
    def __init__(self, *args, **kwrds):
        self._mapping = _SubSymDict(*args, **kwrds)
    def __getitem__(self, key1):
        self._mapping.set_key1(key1)
        return self._mapping
    def __setitem__(self, key1, value):
        raise NotImplementedError
    def __str__(self):
        return '_mapping: ' + self._mapping.__str__()
    def __getattr__(self, name):
        return getattr(self._mapping, name)

class _SubSymDict(dict):
    def __init__(self, *args, **kwrds):
        dict.__init__(self, *args, **kwrds)
    def set_key1(self, key1):
        self.key1 = key1
    def __getitem__(self, key2):
        return dict.__getitem__(self, frozenset((self.key1, key2)))
    def __setitem__(self, key2, value):
        dict.__setitem__(self, frozenset((self.key1, key2)), value)

symdict = SymDict()
symdict[2][4] = 24
symdict[4][2] = 42

print 'symdict[2][4]:', symdict[2][4]
# symdict[2][4]: 42
print 'symdict[4][2]:', symdict[4][2]
# symdict[4][2]: 42
print 'symdict:', symdict
# symdict: _mapping: {frozenset([2, 4]): 42}

print symdict.keys()
# [frozenset([2, 4])]

这篇关于对称字典,其中d [a] [b] == d [b] [a]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆