如何添加或增加 Python Counter 类的单个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30001856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add or increment single item of the Python Counter class
提问by scharfmn
A set
uses .update
to add multiple items, and .add
to add a single one. Why doesn't collections.Counter
work the same way?
Aset
用于.update
添加多个项目,以及.add
添加单个项目。为什么不collections.Counter
以同样的方式工作?
To increment a single Counter
item using Counter.update
, you have to add it to a list:
要使用 增加单个Counter
项目Counter.update
,您必须将其添加到列表中:
c = Counter()
for item in something:
for property in properties_of_interest:
if item.has_some_property: # pseudocode: more complex logic here
c.update([item.property])
elif item.has_some_other_property:
c.update([item.other_property])
# elif... etc
Can I get Counter
to act like set
(i.e. eliminate having to put the property in a list)?
我可以表现Counter
得像set
(即不必将财产列入清单)吗?
Edit:Use Case: Imagine a case where you have some unknown objects, and you're trying many different things quickly to find out some preliminary things about them: performance and scaling don't matter, and a comprehension would make adding and subtracting logic time-consuming.
编辑:用例:想象一个案例,您有一些未知对象,并且您正在快速尝试许多不同的事情以找出有关它们的一些初步信息:性能和缩放无关紧要,理解将使加减逻辑耗时。
采纳答案by shx2
Well, you don't really need to use methods of Counter
in order to count, do you? There's a +=
operator for that, which also works in conjunction with Counter.
好吧,你真的不需要使用 的方法Counter
来计数,对吗?有一个+=
运算符,它也可以与 Counter 一起使用。
c = Counter()
for item in something:
if item.has_some_property:
c[item.property] += 1
elif item.has_some_other_property:
c[item.other_property] += 1
elif item.has_some.third_property:
c[item.third_property] += 1
回答by Christian Aichinger
There is a more Pythonic way to do what you want:
有一种更 Pythonic 的方式来做你想做的事:
c = Counter(item.property for item in something if item.has_some_property)
It uses a generator expressioninstead of open-coding the loop.
它使用生成器表达式而不是对循环进行开放编码。
Edit:Missed your no-list-comprehensions paragraph. I still think this is the way to actually use Counter
in practice. If you have too much code to put into a generator expression or list comprehension, it is often better to factor that into a function and call that from a comprehension.
编辑:错过了您的无列表理解段落。我仍然认为这是Counter
在实践中实际使用的方法。如果您有太多代码要放入生成器表达式或列表推导式中,通常最好将其分解为函数并从推导式中调用。
回答by Vidhya G
>>> c = collections.Counter(a=23, b=-9)
You can add a new element and set its value like this:
您可以添加一个新元素并设置其值,如下所示:
>>> c['d'] = 8
>>> c
Counter({'a': 23, 'd': 8, 'b': -9})
Increment:
增量:
>>> c['d'] += 1
>>> c
Counter({'a': 23, 'd': 9, 'b': -9}
Note though that c['b'] = 0
does not delete:
请注意,虽然这c['b'] = 0
不会删除:
>>> c['b'] = 0
>>> c
Counter({'a': 23, 'd': 9, 'b': 0})
To delete use del
:
删除使用del
:
>>> del c['b']
>>> c
Counter({'a': 23, 'd': 9})
Counter is a dict subclass
Counter 是一个 dict 子类