计数从python字典中读取并解压缩

时间:2017-11-03 00:36:27

标签: python dictionary python-3.5

我有兴趣计算对字典值的访问次数。我不确定如何在计数器中包含字典解包。有什么提示吗?

from collections import defaultdict

class LDict(dict):
    def __init__(self, *args, **kwargs):
        '''
        This is a read-counting dictionary
        '''
        super().__init__(*args, **kwargs)
        self._lookup = defaultdict(lambda : 0)

    def __getitem__(self, key):
        retval = super().__getitem__(key)
        self._lookup[key] += 1
        return retval

    def __setitem__(self, key, value):
        super().__setitem__(key, value)
        self._lookup[key] = self._lookup.default_factory()

    def __delitem__(self, key):
        super().__delitem__(self, key)
        _ = self._lookup[key]
        del self._lookup[key]

    def list_unused(self):
        return [key for key in self if self._lookup[key] == 0]

l = LDict(a='apple', b='bugger')

print({**l, **l})
print(l.list_unused())
_ = l['a']
print(l.list_unused())

1 个答案:

答案 0 :(得分:1)

您需要覆盖更多方法。通过__getitem__() 集中访问:copy()items()等其他方法,无需通过__getitem()__即可访问密钥。我假设**运算符使用items(),但您需要处理所有方法以跟踪每个访问。在许多情况下,你将不得不做出判断。例如,__repr__()是否算作访问权限?返回的字符串包含格式化的每个键和值,所以我认为它确实如此。

我建议覆盖这些方法的所有,因为你也必须在作业上进行簿记。

def __repr__(self):
def __len__(self):
def __iter__(self):
def clear(self):
def copy(self):
def has_key(self, k):
def update(self, *args, **kwargs):
def keys(self):
def values(self):
def items(self):

编辑:显然,这里有一个重要的警告,与您的实施直接相关。如果LDict扩展dict,则在字典解包{ **l, **l}期间不会调用这些方法。

显然,您可以遵循建议here,并在不延长dict的情况下实施LDict。这对我有用:

from collections import MutableMapping

class LDict(MutableMapping):
    def __init__(self, *args, **kwargs):
        '''
        This is a read-counting dictionary
        '''
        self._lookup = defaultdict(lambda : 0)
        self.data = {}
        if kwargs:
            self.data.update(kwargs)

    def __getitem__(self, key):
        retval = self.data[key]
        self._lookup[key] += 1
        return retval

    def __setitem__(self, key, value):
        self.data[key] = value
        self._lookup[key] = self._lookup.default_factory()

    def __delitem__(self, key):
        del self.data[key]
        _ = self._lookup[key]
        del self._lookup[key]

    def items(self):
        print('items is being called!')
        yield from self.data.items()

    def __iter__(self):
        print('__iter__ is being called!')
        yield from self.data

    def __len__(self):
        return len(self.data)    


    def list_unused(self):
        return [key for key in self if self._lookup[key] == 0]

l = LDict(a='apple', b='bugger')

print({**l, **l})
print(l.list_unused())
_ = l['a']
print(l.list_unused())

产生输出:

__iter__ is being called!
__iter__ is being called!
{'b': 'bugger', 'a': 'apple'}
__iter__ is being called!
[]
__iter__ is being called!
[]

(我只实现了最低限度以使示例工作,我仍然建议实现我列出的一组方法,如果你希望你的计数是正确的!)

所以我猜你问题的答案是你必须

  1. 实施__iter__(self)方法
  2. 不要继承dict()