質問するログイン新規登録

回答編集履歴

1

追記

2018/08/06 08:33

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -21,4 +21,35 @@
21
21
  if __name__ == "__main__":
22
22
  main()
23
23
 
24
- ```
24
+ ```
25
+ ---
26
+
27
+ `OrderedDict`と`Counter`を使った順序性を保証するコードです。
28
+ ```Python
29
+ # -*- coding: utf-8 -*-
30
+ from collections import Counter, OrderedDict
31
+ from string import ascii_letters
32
+
33
+ class OrderedCounter(Counter, OrderedDict):
34
+ pass
35
+
36
+ def main():
37
+ alphabet = OrderedCounter(OrderedDict.fromkeys(ascii_letters, 0))
38
+ while True:
39
+ word = input('英単語を入力してください')
40
+ if word == '':
41
+ break
42
+ alphabet.update(*word.split())
43
+
44
+ for key, num in alphabet.items():
45
+ print(f'{key}が{num}個ありました')
46
+
47
+
48
+ if __name__ == "__main__":
49
+ main()
50
+ ```
51
+
52
+ ◇参考情報
53
+ [8.3.6. OrderedDict オブジェクト](https://docs.python.jp/3/library/collections.html#ordereddict-objects)
54
+ [string.ascii_lowercase](https://docs.python.jp/3/library/string.html#string.ascii_lowercase)
55
+ [Creating an Ordered Counter](https://stackoverflow.com/questions/35446015/creating-an-ordered-counter)