Skip to content

Instantly share code, notes, and snippets.

@mickeypash
Last active July 10, 2020 08:01
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 mickeypash/b03b1888bb0015a6ec02fa5359fb6eca to your computer and use it in GitHub Desktop.
Save mickeypash/b03b1888bb0015a6ec02fa5359fb6eca to your computer and use it in GitHub Desktop.
counter = {}
for word in words:
counter[word] = counter.get(word, 0) + 1 # we basically count the words in the dict e.g. {'mickey': 1, 'alex': 2, 'anna': 2}
freq = list(counter.keys()) # we set up a list e.g. freq = ['mickey', 'alex', 'anna']
freq.sort(key = lambda x : (-counter[x], x)) # the intention here is to sort by frequency in descending oder 2,1,0...
# .sort (https://docs.python.org/3.3/library/stdtypes.html#list.sort)
# Sort the freq list in place
# takes a key-word argument called 'key' to be used for a comparison function
# lambda (https://docs.python.org/3/reference/expressions.html#lambda)
# is an anonymous function (meaning it doesn't have a name)
# you could have done def compare(key): return (-counter[x], x) but you can see it's more verbose
# -counter[x] -> this basically takes a key like 'mickey' and grabs the associated value 1
# and negates it so it becomes -1, we do this so we can have descending order (-2, -1, 0)
# as by default the sorting is in ascending order (0, 1, 2); this can be achieved by passid the 'reversed' keyword arg
# The function returns a tuple (-counter[x], x), this is so that
# if we have the same frequency (-2, 'mickey'), (-2, 'anna') we can sort on the second element
# Tuples are sorted lexicographically, starting with the first item then the second etc.
# the resulting freq list will be sorted by frequency of occurrence followed by lexicographically
# so ['alex', 'anna', 'mickey']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment