📜  在python代码示例中创建一组计数器的get方法

📅  最后修改于: 2022-03-11 14:45:05.745000             🧑  作者: Mango

代码示例1
book_title =  ['great', 'expectations','the', 'adventures', 'of', 'sherlock','holmes','the','great','gatsby','hamlet','adventures','of','huckleberry','fin']
word_counter = {}

for word in book_title:
    word_counter[word] = word_counter.get(word, 0) + 1

# Output
# {'great': 2, 'expectations': 1, 'the': 2, 'adventures': 2, 'of': 2, 'sherlock': 1, 'holmes': 1, 'gatsby': 1, 'hamlet': 1, 'huckleberry': 1, 'fin': 1}