We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Collections
- Word Order
- Discussions
Word Order
Word Order
Sort by
recency
|
1588 Discussions
|
Please Login in order to post a comment
from collections import Counter n = int(input()) l = [] for i in range(n): word = input() l.append(word) val = Counter(l) print(len(val.keys())) for x,y in val.items(): print(y, end=' ')
Before Python 3.7, the order is not guaranteed in dictionaries:
Since Python 3.7, the order is guaranteed to be kept in dictionaries which makes the code even simpler:
Use OrderedDict to preserve the order of input words. Loop over input lines, setting each word's count to zero initially. Update each word's count as it appears. Finally, print the count of unique words and their occurrences.
Here's my codes.
using native:
with Counter func: