首页 > 解决方案 > 如何计算列表中每个字母的 cout?

问题描述

我有一个大约 39000 字的列表。我需要计算列表中每个字母的出现次数,并将它们存储在字典中,其中字母为键,出现次数为值。怎么做?

有问题的清单是

['voluptuous',
 'outbreak',
 'starched',
 'sharpest',
 'widens',
 'briefcase',
 'stag',
 'gracias',
 'complexes',
 'magnum',
 'classifying',
 'eloquent',
 'forecasters',
 'shepherd',
 'vestments',
 'indestructible',
 'chartres',
 'condemning',
 'closet',
 'davis',
 'students',
.
.
.

所以,预期的输出应该是这样的

{'a': 2433,
 'b': 5717,
 'c': 1236,
 'd': 12255,
 'e': 35170,
 'f': 4118,
 'g': 8630,
 'h': 7327,
 'i': 26075,
 'j': 6430,
 'k': 2965,
 'l': 16703,
 'm': 8672,
 'n': 22630,
 'o': 19199,
 'p': 8543,
 'q': 5325,
 'r': 22104,
 's': 23730,
 't': 20649,
 'u': 10196,
 'v': 3427,
 'w': 2799,
 'x': 828,
 'y': 5344,
 'z': 1031}

标签: python-3.x

解决方案


这是使用的变体collections.Counter

from collections import Counter

counter = Counter()

words = ['voluptuous',
 'outbreak',
 'starched',
 'sharpest',
 'widens',
 'briefcase',
 'stag',
 'gracias',
 'complexes',
 'magnum',
 'classifying',
 'eloquent',
 'forecasters',
 'shepherd',
 'vestments',
 'indestructible',
 'chartres',
 'condemning',
 'closet',
 'davis',
 'students']

for word in words:
    counter += Counter(word)

或在一行中:

counter = Counter(char for word in words for char in word)

推荐阅读