本文整理匯總了Python中collections.Counter.__setitem__方法的典型用法代碼示例。如果您正苦於以下問題:Python Counter.__setitem__方法的具體用法?Python Counter.__setitem__怎麽用?Python Counter.__setitem__使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類collections.Counter
的用法示例。
在下文中一共展示了Counter.__setitem__方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __setitem__
# 需要導入模塊: from collections import Counter [as 別名]
# 或者: from collections.Counter import __setitem__ [as 別名]
def __setitem__(self, item, val):
if len(self) > self._max * 2:
print("Purging")
to_delete = self.most_common()[self._max:]
for ii in to_delete:
del self[ii]
Counter.__setitem__(self, item, val)
示例2: __setitem__
# 需要導入模塊: from collections import Counter [as 別名]
# 或者: from collections.Counter import __setitem__ [as 別名]
def __setitem__(self, k, v):
if k in self:
assert self[k]==v
return
if self._worst is not None: # the beam is full
if v<=self._worst[1]:
return
else:
del self[self._worst[0]]
Counter.__setitem__(self, k, v)
assert self._size>=len(self)
if self._size==len(self):
self._worst = min(self.items(), key=lambda (k1,v1): v1)
示例3: __setitem__
# 需要導入模塊: from collections import Counter [as 別名]
# 或者: from collections.Counter import __setitem__ [as 別名]
def __setitem__(self, *args, **kwargs):
if self.__finishedinit:
raise AttributeError("Can't change a frozen counter!")
Counter.__setitem__(self, *args, **kwargs)
示例4: __setitem__
# 需要導入模塊: from collections import Counter [as 別名]
# 或者: from collections.Counter import __setitem__ [as 別名]
def __setitem__(x, i, y):
"""x.__setitem__(i, y) <==> x[i]=y"""
if y == 0:
del x[i]
else:
Counter.__setitem__(x, i, y)