找到你要的答案

Q:Python Counter class: add or increment single item

Q:Python计数器类:添加或增加单项

To increment a single item in collections.Counter, you have to add it to a list to use 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

A set uses .update to add multiple items, and add to add a single one. Why doesn't Counter work the same way? Can I get it to act similarly (i.e. eliminate having to put the property in a list)?

Imagine 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.

在集合中单个项目的增量。柜台,你必须将它添加到一个列表来使用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

一个集使用。更新以添加多个项目,并添加添加单个项目。为什么柜台工作不一样?我可以让它采取同样的行动(即消除必须在列表中的财产)?

想象一下,当你有一些未知的对象,你尝试很多不同的东西很快就发现了一些关于他们的初步事情:性能和缩放无关紧要,理解将使增加和减去逻辑耗时。

answer1: 回答1:

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.

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

你真的不需要用计数器的方法来数数,是吗?有一个+运算符,这也与计数器一起工作。

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
answer2: 回答2:

There is a more Pythonic way to do what you want:

c = Counter(item.property for item in something if item.has_some_property)

It uses a generator expression instead 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.

还有一个更Pythonic的去做你想做的:

c = Counter(item.property for item in something if item.has_some_property)

它使用一个生成器表达式而不是循环编码。

编辑:错过你的任何列表推导式的段落。我仍然认为这是实际使用计数器的方法。如果你有太多的代码放在一个生成器表达式或列表理解,它往往是更好的因素到一个函数,并调用从理解。

answer3: 回答3:
>>> 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
Counter({'a': 23, 'd': 9, 'b': 0})

To delete use del:

>>> del c['b']
>>> c
Counter({'a': 23, 'd': 9})

Counter is a dict subclass

>>> c = collections.Counter(a=23, b=-9)

您可以添加新元素并将其设置为此值:

>>> c['d'] = 8
>>> c
Counter({'a': 23, 'd': 8, 'b': -9})

增量:

>>> c['d'] += 1
>>> c
Counter({'a': 23, 'd': 9, 'b': -9} 

需要注意的是,C [ b ] = 0不删除:

>>> c['b'] = 0
>>> c
Counter({'a': 23, 'd': 9, 'b': 0})

删除使用删除:

>>> del c['b']
>>> c
Counter({'a': 23, 'd': 9})

计数器是一个字典类

python  counter