【发布时间】:2020-07-30 17:51:44
【问题描述】:
words = ['rocky','mahesh','surendra','mahesh','rocky','mahesh','deepak','mahesh','mahesh','mahesh','surendra']
words_count = {}
for word in words:
words_count[word] = words_count.get(word, 0) + 1
print(words_count)
# Expected Output
# {'rocky': 2, 'mahesh': 6, 'surendra': 2, 'deepak': 1}
在这里,我想使用 dict/list 理解获得相同的输出。 期待简单而简短的代码。
【问题讨论】:
标签: python list-comprehension dictionary-comprehension