出現数を辞書型でカウントする(counterを使わない)
| 登録日 | :2023/10/20 05:11 |
|---|---|
| カテゴリ | :Python基礎 |
ライブラリのcounterを使わずに、出現回数をカウントする方法をメモする
words = ['python', 'c', 'java', 'go', 'python', 'c', 'go', 'python']
d = {}
for word in words:
d[word] = d.get(word, 0) + 1
print(d)
実行結果
{'python': 3, 'c': 2, 'java': 1, 'go': 2}