-4

I don't know if my question is repeated or not since I couldn't really find the right answer. I wanted to print the most frequent words or numbers on a new line. However, if the frequency are the same then print both words/numbers.

Input: green green green orange orange yellow

Output: green


Input: green green green orange orange orange yellow

Output: green 
        orange


Input: 1 1 2 2 3 3 4

Output: 1
        2
        3


Input: 1 1 1 2 2 3 3

Output: 1
Anon123
  • 1
  • 4

2 Answers2

2

can you tell what you have tried? follow snippet may help you a bit

 words = ['green', 'green','green', 'yellow']

 from collections import Counter
 counts = Counter(words)

 top = [k for k, _ in   counts.most_common(list(counts.values()).count(max(counts.values())))]
 print(top)
magegaga.com
  • 500
  • 3
  • 7
0

You could also do max with key argument, then list comprehension takes all that has the count of that, because max just takes one:

>>> words = ['green', 'green','green', 'yellow','orange','orange','orange']
>>> list(set([i for i in words if words.count(i) == words.count(max(words,key=words.count))]))
['green', 'orange']
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114