從Adding a single character to add keys in Counter,@AshwiniChaudhary給了一個很好的答案,創建一個新的Counter
對象有不同的set()函數:創建具有特殊設定功能自定義計數器對象
from collections import Counter
class CustomCounter(Counter):
def __setitem__(self, key, value):
if len(key) > 1 and not key.endswith(u"\uE000"):
key += u"\uE000"
super(CustomCounter, self).__setitem__(key, value)
要允許用戶自定義字符/ STR追加到重點,我已經試過:
from collections import Counter, defaultdict
class AppendedStrCounter(Counter):
def __init__(self, str_to_append):
self._appended_str = str_to_append
super(AppendedStrCounter, self).__init__()
def __setitem__(self, key, value):
if len(key) > 1 and not key.endswith(self._appended_str):
key += self._appended_str
super(AppendedStrCounter, self).__setitem__(tuple(key), value)
但它返回一個空計數器:
>>> class AppendedStrCounter(Counter):
... def __init__(self, str_to_append):
... self._appended_str = str_to_append
... super(AppendedStrCounter, self).__init__()
... def __setitem__(self, key, value):
... if len(key) > 1 and not key.endswith(self._appended_str):
... key += self._appended_str
... super(AppendedStrCounter, self).__setitem__(tuple(key), value)
...
>>> AppendedStrCounter('foo bar bar blah'.split())
AppendedStrCounter()
那是因爲我的思念在__init__()
的ITER:
from collections import Counter, defaultdict
class AppendedStrCounter(Counter):
def __init__(self, iter, str_to_append):
self._appended_str = str_to_append
super(AppendedStrCounter, self).__init__(iter)
def __setitem__(self, key, value):
if len(key) > 1 and not key.endswith(self._appended_str):
key += self._appended_str
super(AppendedStrCounter, self).__setitem__(tuple(key), value)
[出]:
>>> AppendedStrCounter('foo bar bar blah'.split(), u'\ue000')
AppendedStrCounter({('f', 'o', 'o', '\ue000'): 1, ('b', 'a', 'r', '\ue000'): 1, ('b', 'l', 'a', 'h', '\ue000'): 1})
但'bar'
值是錯誤的,它應該是2,而不是1
正在使用iter
到__init__()
正確的方式來初始化Counter
?
您製作的超類構造函數使用'__setitem__'爲它增加了每個項目的假設,但沒有保證它必須。https://docs.python.org/2/library/collections.html#collections.Counter只承諾它的缺點tructor會表現,而不是如何實施。 – amalloy
仔細查看@AshwiniChaudhary的參考答案。在他的答案中,「the」鍵的計數器也是1而不是2 – Felix
更改存儲鍵的方式可能會帶來一些令人討厭的驚喜...例如,沒有人可以存儲「word」ue000「計數與'CustomCounter'中的''word''分開。另外,他們如何獲得特定的詞語?用戶必須記得每當他們需要cc ['word']'時要求'cc ['word \ ue000']',這完全破壞了封裝的OOP目標。 –