How to add or increment single item of the Python

2020-02-27 08:05发布

问题:

A set uses .update to add multiple items, and .add to add a single one. Why doesn't collections.Counter work the same way?

To increment a single Counter item using Counter.update, you have to add it to a list:

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)?

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.

回答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


回答2:

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



回答3:

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.