如果您像这样比较两个计数器:
counter_a == counter_b
你得到真或假。reduce
但是,如果您要使用/比较它们operator.eq
,则会得到 False。有人可以告诉我这里发生了什么吗?
这是一个例子:
>>> import collections
>>> import operator
>>> a = ['ab', 'ab', 'ab']
>>> b = ['bc', 'ab', 'something']
>>> counters_a = map(collections.Counter, a)
[Counter({'a': 1, 'b': 1}), Counter({'a': 1, 'b': 1}), Counter({'a': 1, 'b': 1})]
>>> counters_b = map(collections.Counter, b)
[Counter({'c': 1, 'b': 1}), Counter({'a': 1, 'b': 1}), Counter({'e': 1, 'g': 1, 'i': 1, 'h': 1, 'm': 1, 'o': 1, 'n': 1, 's': 1, 't': 1})]
>>> counters_a[0] == counters_a[1]
True
>>> counters_b[0] == counters_b[1]
False
>>> reduce(operator.eq, [1, 1, 1])
True
>>> reduce(operator.eq, [1, 1, 2])
False
>>> reduce(operator.eq, counters_b)
**False**
>>> reduce(operator.eq, counters_a)
**False**