11
我期待總結一個python中的計數器列表。例如,要總結:在Python中計數器的總結列表
counter_list = [Counter({"a":1, "b":2}), Counter({"b":3, "c":4})]
給Counter({'b': 5, 'c': 4, 'a': 1})
我可以得到下面的代碼做了總結:
counter_master = Counter()
for element in counter_list:
counter_master = counter_master + element
但我很困惑,爲什麼counter_master = sum(counter_list)
導致錯誤TypeError: unsupported operand type(s) for +: 'int' and 'Counter'
?鑑於可以將計數器加在一起,爲什麼不可能將它們相加?