[Python-ideas] A meaningful `if counter:`

MRAB python at mrabarnett.plus.com
Tue Apr 5 23:58:21 CEST 2011


On 05/04/2011 22:46, cool-RR wrote:
> On Tue, Apr 5, 2011 at 11:34 PM, Bruce Leban <bruce at leapyear.org
> <mailto:bruce at leapyear.org>> wrote:
>
>     According to the documentation
>     http://docs.python.org/release/3.1.3/library/collections.html#collections.Counter.elements
>     c.elements() returns an empty iterator if there are no elements with
>     counts > 0.
>
>
> I am aware of the current behavior.
>
>     When you say you want it to be smart, what do you mean besides that?
>
>
> I mean that it will be like `dict.keys`; an object which behaves
> similarly to a list but is implemented more smartly. e.g.,
> `len(counter.elements())` would be implemented as
> `sum(counter.values())` for better O complexity, and
> `counter.elements().__bool__` would be (trivially) implemented. (You
> currently can't do a meaningful `if counter.elements()`).
>
A count in a Counter can be negative, so `len(counter.elements())`
isn't the same as `sum(counter.values())` (counter.elements() returns
only those elements having a positive count).



More information about the Python-ideas mailing list