0

So I have found the frequencies of numbers from a list and have created a list such as this [2:3 , 25:1, 22:4, 5:2, 5:2] What im trying to do after that is have a for loop detecting the maximum frequency (different numbers can have the same frequencies) and then printing the number and frequency that is the highest which may be more than one.

bluelantern
  • 1,639
  • 2
  • 12
  • 7

1 Answers1

1

Depending on how your data is structured

>>> data = {2:3 , 25:1, 22:4, 5:2, 5:2}
>>> max(data, key = lambda x: data[x])
22

or

>>> data = [(2, 3), (25, 1), (22, 4), (5,2), (5,2)]
>>> max(data, key = lambda x: x[1])
(22, 4)

should do the trick.

[Edit]

>>> data = {2:3 , 25:4, 22:4, 5:2, 5:2}
>>> max_key = max(data.values())
>>> print [i for i in data if data[i] == max_key]
[22, 25]
luke14free
  • 2,529
  • 1
  • 17
  • 25
  • Lets say you have data = {2:4, 5:4, 3:2, 22:6} how would you display 2 and 5 – bluelantern May 30 '12 at 08:24
  • 1
    Edited, this will take into account multiple values. You are using a bad data structure though. I strongly suggest you to take a look at http://docs.python.org/dev/library/collections.html#collections.Counter – luke14free May 30 '12 at 08:29