我需要呈现文本中出现的字母。如果其中一个字母没有出现,它应该显示为零,并且输出也应该按字母顺序排序。我准备了以下 Python 代码,我的问题是如何为未出现的字母显示零以及如何根据列表键对列表值进行排序以对输出进行排序?
fdist = Counter(c for c in f.lower() if c.isalpha())
print sorted(fdist.items()) #only to show the output details
print fdist.values()
示例文本的输出如下所示:
[('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]
但输出应如下所示:
[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]