Task: The first line contains the integer, N. The next N lines each contain a word. Output should be: 1) On the first line, output the number of distinct words from the input. 2) On the second line, output the number of occurrences for each distinct word according to their appearance in the input. I had no difficulty with #1. For point #2, I used Counter to get the occurrences of the words. However I am having difficulty in printing them in the order which it was received. Below is my code.
from collections import Counter
from collections import OrderedDict
all_words=[]
for _ in range(int(raw_input())):
name=raw_input()
all_words.append(name)
uniqlst=list(set(all_words))
print len(uniqlst)##On the first line, output the number of distinct words from the input.
x=OrderedDict(Counter(all_words)) #This is where I am having trouble to get values of x in the order it was received.
print " ".join(map(str,x.values()))
Input:
4
bcdef
abcdef
bcde
bcdef
Output of my code:
3
1 1 2
Expected output:
3
2 1 1