collections.OrderedDictを使うと良いでしょう。
Python
1from collections import OrderedDict
2
3data = input().split()
4a = OrderedDict()
5
6for s in data:
7 if s in a:
8 a[s] += 1
9 else:
10 a[s] = 1
11
12for s, num in a.items():
13 print(s, num)
Pythonチュートリアルで紹介されているOrderedCounterを使う手もあります。
何度も同じような処理をするならこちらの方がより良いです。
Python
1from collections import Counter, OrderedDict
2
3class OrderedCounter(Counter, OrderedDict):
4 'Counter that remembers the order elements are first encountered'
5
6 def __repr__(self):
7 return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
8
9 def __reduce__(self):
10 return self.__class__, (OrderedDict(self),)
11
12data = input().split()
13a = OrderedCounter(data)
14
15for s, num in a.items():
16 print(s, num)
17
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/30 05:39