I read data from a bunch or emails and count frequency of each word. first construct two counters:
counters.stats = collections.defaultdict(dict)
The key of stats is word. For each word, I construct a dict, whose key is the name of the email and value is the frequency of that word in this email.
Now I have a list which contains those keys in counters.stats, by in a different order. I want to sort the key in ‘stats’ by the list.
def print_stats(counters):
for form, cat_to_stats in sorted(counters.stats.items(), key = chi_sort):
How to build the function chi_sort? Or other methods?
Assuming that the values in
Lonly occur once:where
Lrefers to your list.If this yields the values in reverse order, you can fix that by adding
reversed=Trueto yoursortedcall.