辞書包括表記
|
{ key_item: value_item for in iterable }
word = 'letters'
letter_counts = {letter: word.count(letter) for letter in word}
letter_counts
{'l': 1, 'e': 2, 't': 2, 'r': 1, 's': 1, }
無駄をなくす
word = 'letters'
letter_counts = {letter: word.count(letter) for letter in set(word)}
letter_counts
{'l': 1, 'e': 2, 't': 2, 'r': 1, 's': 1, }