Python 计数器字典中的求和值

Python 计数器字典中的求和值,python,python-3.x,dictionary,counter,Python,Python 3.x,Dictionary,Counter,我有一本字典,它显示了一部电影剧本中“女人”说了多少个词。现在我不想把字典的价值加起来,所以我得到了“女人”所说的单词的总数。 有没有办法做到这一点 'WOMAN': Counter({'Mia': 4, 'a': 4, 'the': 4, 'and': 3, 'is': 3,

我有一本字典,它显示了一部电影剧本中“女人”说了多少个词。现在我不想把字典的价值加起来,所以我得到了“女人”所说的单词的总数。 有没有办法做到这一点

'WOMAN': Counter({'Mia': 4,
                           'a': 4,
                           'the': 4,
                           'and': 3,
                           'is': 3,
                           'on': 3,
                           '--': 3,
                           'it': 2,
                           'Then': 2,
                           'STUDIO': 2,
                           'Cappuccino,': 1,                              
                           'from': 1,
                           'her.': 1,
                           'No,': 1,
                           'I': 1,
                           'insist.': 1,
                           'She': 1,
                           'pays.': 1,
                           'smiles': 1,
                           'at': 1,
                           'drops': 1,
                           'bill': 1,
                           'in': 1,
                           'tip': 1,
                           'jar.': 1,
                           'watches': 1,
                           'as': 1,
                           'Woman': 1,
                           'walks': 1,
                           'off,': 1,
                           'joined': 1,
                           'screen:': 1,
                           '4:07.': 1})})
从文档中,您可以使用
sum(Counter.values())


假设必须事先拆分字符串以提取单词,则可以在早期阶段计算单词数:

from collections import Counter

x = 'this is a test string with this string repeated'

words_split = x.split()

count, c = len(words_split), Counter(words_split)

print(count)
# 9

print(c)
# Counter({'this': 2, 'string': 2, 'is': 1, 'a': 1, 'test': 1, 'with': 1, 'repeated': 1})
sum(计数器({…}).values())
from collections import Counter

x = 'this is a test string with this string repeated'

words_split = x.split()

count, c = len(words_split), Counter(words_split)

print(count)
# 9

print(c)
# Counter({'this': 2, 'string': 2, 'is': 1, 'a': 1, 'test': 1, 'with': 1, 'repeated': 1})