Re:A few things to remember while coding in Python
by LukeStebbing on Saturday May 19, 2012 @05:25PM
(#3997118)
Or just say:
I was surprised to see that missing, given that Counter was mentioned in the next section.
[ Parent
| on HN ]
Re:A few things to remember while coding in Python
by anthonyb on Saturday May 19, 2012 @09:27PM
(#3997622)
Ah, Guido's time machine strikes again :)
Here's my (now mostly obsolete) version:
def count(string):
counts = {}
for item in set(string):
counts[item] = string.count(item)
return counts
print count("abracadabra")
I had a look into the collections library, and it just uses iterable.iteritems(). I suspect that this might be faster for larger strings with multiple repeating characters, since set() and count() will pass the string directly to C.