Skip to content

Instantly share code, notes, and snippets.

@milin
Created April 29, 2011 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milin/947607 to your computer and use it in GitHub Desktop.
Save milin/947607 to your computer and use it in GitHub Desktop.
def comma_sep(bits):
count = dict()
orderedCounts = list() # counts, ordered
for bit in bits:
# if key exists
if count.has_key(bit):
count[bit] += 1
else: # key does not exist
count[bit] = 1
string = ""
# sort by count
orderedCounts = count.items()
orderedCounts.sort(cmp=lambda x,y: cmp(x[1], y[1]))
orderedCounts.reverse()
for bit in orderedCounts:
# concatinate the strings
string += ", %s %i" % bit[0], count[bit[0]]
return string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment