Here is the corrected version (thanks for pointing out the errors)
def frequency(s): return ''.join( [k for k, v in sorted( reduce( lambda d, c: d.update([[c, d.get(c, 0) + 1]]) or d, list(s), dict()).items(), lambda a, b: cmp(a[1], b[1]), reverse=True)])
I think using reduce makes a difference in this resolution compared to others ...
In action:
>>> from frequency import frequency >>> frequency('abbbccddddxxxyyyyyz') 'ydbxcaz'
This one includes key extraction (and counting them) as well !!! Another nice property is the initialization of the dictionary on the same line :)
Also: no includes , just embedded.
The reduce function is pretty hard to wrap around, and the meaning of the dictionary in lambda also a bit cumbersome in python, but, well, it works!
source share