python counters

in my pythonic trolling of the internet, i found a character counter, written by ashoksk. given a string of text, return a count for each character in the string (a tool that might be used when attempting to crack an encrypted message)

so i wondered, could this be a python one liner? turns out that, from python 2.7 on, it can be (highlight to see the code below):


from collections import Counter
def fn(x): c = Counter(x); return {x: c[x] for x in c.keys()}

it’s a cheat, though. anyone have a real one-line implementation of this?


[More programming riddles]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s