itertools py3.4 - filter list using not equal - fails as bool

Sayth Renshaw flebber.crue at gmail.com
Wed May 13 09:58:31 EDT 2015


Thank You for the explanations.

I found this counter implementation is really cool and easily adaptable to more solutions.

Thanks
> Alternatively collections.Counter() supports an arbitrary number of bins...
> 
> >>> import collections
> >>> freq = collections.Counter(t[1] for t in stats)
> >>> freq
> Counter({1: 12, 2: 12, 3: 12, 4: 12, 5: 12})
> 
> ...but you can easily reduce them:
> 
> >>> freq = collections.Counter(t[1] == 1 for t in stats)
> >>> freq
> Counter({False: 48, True: 12})
> >>> one = freq[True]
> >>> total = sum(freq.values())
> >>> print("{} of {} ({}%) have t[1] == 1".format(one, total, one/total*100))
> 12 of 60 (20.0%) have t[1] == 1




More information about the Python-list mailing list