collections.Counter,有什么办法可以避免添加字符串值吗?
collections.Counter, is there any way to avoid adding string values?
比如我有两本字典。
>>> dict_a = {'total': 20, 'remaining': 10, 'group': 'group_a'}
>>> dict_b = {'total': 30, 'remaining': 29, 'group': 'group_a'}
我正在使用 collections.Counter 进行计数。
>>> dict_c = Counter()
>>> dict_c.update(Counter(dict_a))
>>> dict_c.update(Counter(dict_b))
>>> print(dict_c)
{'toal': 50, 'remaining': 39, 'group': 'group_agroup_a'}
有什么方法可以只添加整数类型的值吗?即,当添加时,它仅累加整数类型值。
>>> print(dict_c)
>>> {'toal': 50, 'remaining': 39, 'group': 'group_a'}
Is there any way to add only integer type values?
这可能不是最有效的解决方案,但您可以简单地遍历 dict_c
的键值对来检查值是否属于 int
类型,从而创建一个新字典仅包含整数值。
from collections import Counter
dict_a = {'total': 20, 'remaining': 10, 'group': 'group_a'}
dict_b = {'total': 30, 'remaining': 29, 'group': 'group_a'}
dict_c = Counter(dict_a) + Counter(dict_b)
dict_result = {key: value for key, value in dict_c.items() if isinstance(value, int)}
print(dict_result)
这returns预期的结果:
{'total': 50, 'remaining': 39}
您可以定义自己的函数来添加两个 Counter
对象,就像您在问题中所拥有的那样。这是必要的,因为添加 Counter
对象的默认方法无法处理其中的非数字值,就像您放入自己的那样。
from collections import Counter
def add_counters(a, b):
""" Add numerical counts from two Counters. """
if not isinstance(a, Counter) or not isinstance(a, Counter):
return NotImplemented
result = Counter()
for elem, count in a.items():
newcount = count + b[elem]
try:
if newcount > 0:
result[elem] = newcount
except TypeError:
result[elem] = count # Just copy value.
for elem, count in b.items():
if elem not in a and count > 0:
result[elem] = count
return result
dict_a = {'total': 20, 'remaining': 10, 'group': 'group_a'}
dict_b = {'total': 30, 'remaining': 29, 'group': 'group_a'}
dict_c = add_counters(Counter(dict_a), Counter(dict_b))
print(dict_c) # -> Counter({'total': 50, 'remaining': 39, 'group': 'group_a'})
请注意,上述内容可能并不完全正确,因为第一个 Counter
参数 a
中的任何非数字项目刚刚复制到结果可能会被第二个 [=15] 覆盖=] 循环,因此它们的最终值是第二个 Counter
中名为 b
的任何值。之所以这样,是因为您没有准确定义在这种情况下您希望发生什么。
比如我有两本字典。
>>> dict_a = {'total': 20, 'remaining': 10, 'group': 'group_a'}
>>> dict_b = {'total': 30, 'remaining': 29, 'group': 'group_a'}
我正在使用 collections.Counter 进行计数。
>>> dict_c = Counter()
>>> dict_c.update(Counter(dict_a))
>>> dict_c.update(Counter(dict_b))
>>> print(dict_c)
{'toal': 50, 'remaining': 39, 'group': 'group_agroup_a'}
有什么方法可以只添加整数类型的值吗?即,当添加时,它仅累加整数类型值。
>>> print(dict_c)
>>> {'toal': 50, 'remaining': 39, 'group': 'group_a'}
Is there any way to add only integer type values?
这可能不是最有效的解决方案,但您可以简单地遍历 dict_c
的键值对来检查值是否属于 int
类型,从而创建一个新字典仅包含整数值。
from collections import Counter
dict_a = {'total': 20, 'remaining': 10, 'group': 'group_a'}
dict_b = {'total': 30, 'remaining': 29, 'group': 'group_a'}
dict_c = Counter(dict_a) + Counter(dict_b)
dict_result = {key: value for key, value in dict_c.items() if isinstance(value, int)}
print(dict_result)
这returns预期的结果:
{'total': 50, 'remaining': 39}
您可以定义自己的函数来添加两个 Counter
对象,就像您在问题中所拥有的那样。这是必要的,因为添加 Counter
对象的默认方法无法处理其中的非数字值,就像您放入自己的那样。
from collections import Counter
def add_counters(a, b):
""" Add numerical counts from two Counters. """
if not isinstance(a, Counter) or not isinstance(a, Counter):
return NotImplemented
result = Counter()
for elem, count in a.items():
newcount = count + b[elem]
try:
if newcount > 0:
result[elem] = newcount
except TypeError:
result[elem] = count # Just copy value.
for elem, count in b.items():
if elem not in a and count > 0:
result[elem] = count
return result
dict_a = {'total': 20, 'remaining': 10, 'group': 'group_a'}
dict_b = {'total': 30, 'remaining': 29, 'group': 'group_a'}
dict_c = add_counters(Counter(dict_a), Counter(dict_b))
print(dict_c) # -> Counter({'total': 50, 'remaining': 39, 'group': 'group_a'})
请注意,上述内容可能并不完全正确,因为第一个 Counter
参数 a
中的任何非数字项目刚刚复制到结果可能会被第二个 [=15] 覆盖=] 循环,因此它们的最终值是第二个 Counter
中名为 b
的任何值。之所以这样,是因为您没有准确定义在这种情况下您希望发生什么。