python - Sorting letter occurrences in a text alphabetically -
i need present occurrences of letters in text. if 1 of letters doesn't occur, should show 0 , output should sorted alphabetically. have prepared following python code, questions how can show 0 non-occurring letters , how can sort list values based on list keys sort output?
fdist = counter(c c in f.lower() if c.isalpha()) print sorted(fdist.items()) #only show output details print fdist.values()
the output sample text this:
[('a', 46), ('b', 5), ('c', 11), ('d', 22), ('e', 76), ('f', 13), ('g', 7), ('h', 29), ('i', 30), ('j', 1), ('k', 6), ('l', 21), ('m', 11), ('n', 34), ('o', 31), ('p', 6), ('q', 1), ('r', 24), ('s', 32), ('t', 52), ('u', 7), ('v', 2), ('w', 10), ('y', 11)] [46, 11, 5, 76, 22, 7, 13, 30, 29, 6, 1, 11, 21, 31, 34, 1, 6, 32, 24, 7, 52, 10, 2, 11]
but output should this:
[46, 5, 11, 22, 76, 13, 7, 29, 46, 1, 6, 21, 11, 34, 31, 6, 1, 24, 32, 52, 7, 2, 10, 0, 11, 0]
something (ignoring sorting part now) ?
import string result = [fdist.get(l, 0) l in string.letters]
or
result = [fdist.get(l, 0) l in string.ascii_lowercase]
since you're dealing lowercase characters
Comments
Post a Comment