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

回答編集履歴

1

追記

2017/11/30 03:39

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -13,4 +13,27 @@
13
13
 
14
14
  for s, num in a.items():
15
15
  print(s, num)
16
+ ```
17
+
18
+ ---
19
+ Pythonチュートリアルで紹介されている[OrderedCounter](https://docs.python.jp/3/library/collections.html#ordereddict-examples-and-recipes)を使う手もあります。
20
+ 何度も同じような処理をするならこちらの方がより良いです。
21
+ ```Python
22
+ from collections import Counter, OrderedDict
23
+
24
+ class OrderedCounter(Counter, OrderedDict):
25
+ 'Counter that remembers the order elements are first encountered'
26
+
27
+ def __repr__(self):
28
+ return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
29
+
30
+ def __reduce__(self):
31
+ return self.__class__, (OrderedDict(self),)
32
+
33
+ data = input().split()
34
+ a = OrderedCounter(data)
35
+
36
+ for s, num in a.items():
37
+ print(s, num)
38
+
16
39
  ```