回答編集履歴

1

追記

2018/08/06 08:33

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -45,3 +45,65 @@
45
45
 
46
46
 
47
47
  ```
48
+
49
+ ---
50
+
51
+
52
+
53
+ `OrderedDict`と`Counter`を使った順序性を保証するコードです。
54
+
55
+ ```Python
56
+
57
+ # -*- coding: utf-8 -*-
58
+
59
+ from collections import Counter, OrderedDict
60
+
61
+ from string import ascii_letters
62
+
63
+
64
+
65
+ class OrderedCounter(Counter, OrderedDict):
66
+
67
+ pass
68
+
69
+
70
+
71
+ def main():
72
+
73
+ alphabet = OrderedCounter(OrderedDict.fromkeys(ascii_letters, 0))
74
+
75
+ while True:
76
+
77
+ word = input('英単語を入力してください')
78
+
79
+ if word == '':
80
+
81
+ break
82
+
83
+ alphabet.update(*word.split())
84
+
85
+
86
+
87
+ for key, num in alphabet.items():
88
+
89
+ print(f'{key}が{num}個ありました')
90
+
91
+
92
+
93
+
94
+
95
+ if __name__ == "__main__":
96
+
97
+ main()
98
+
99
+ ```
100
+
101
+
102
+
103
+ ◇参考情報
104
+
105
+ [8.3.6. OrderedDict オブジェクト](https://docs.python.jp/3/library/collections.html#ordereddict-objects)
106
+
107
+ [string.ascii_lowercase](https://docs.python.jp/3/library/string.html#string.ascii_lowercase)
108
+
109
+ [Creating an Ordered Counter](https://stackoverflow.com/questions/35446015/creating-an-ordered-counter)