Home > database >  Return the most frequent IP address from the logs
Return the most frequent IP address from the logs

Time:11-06

Given a list of logs with IP addresses in the following format.

lines = ["10.0.0.1 - GET 2020-08-24", "10.0.0.1 - GET 2020-08-24", "10.0.0.2 - GET 2020-08-20"]

Return the most frequent IP address from the logs. The retuned IP address value must be in a string format. If multiple IP addresses have the count equal to max count, then return the address as a comma-separated string with IP addresses in sorted order.

Can someone explain to me how to solve this in python using hashmap?

CodePudding user response:

Finding the most occurring element in a list using a hashmap


line 3: Count the occurrences of items in lines using dictionary comprehension and the count() method.

line 4: Sort by values in reverse order (largest to smallest) using dictionary comprehension.

{key: value for (key, value) in iterable}

line 5: We use list comprehension to see if other elements of the directory do not have the same value as the first key (the one with the highest value)

next(iter(cnt.values())) to get the first key value in a dictionary in Python.

dict.values() returns a view object of the values in the dictionary, iter(object) returns an iterator of the values, next(iterator) returns the first value from the iterator.

lines = ["10.0.0.1 - GET 2020-08-24", "10.0.0.1 - GET 2020-08-24", "10.0.0.2 - GET 2020-08-20"]

cnt = {x:lines.count(x) for x in set(lines)}
cnt = {k: cnt[k] for k in sorted(cnt, key=cnt.get, reverse=True)}
res = [k for k in cnt if cnt[k] == next(iter(cnt.values()))]
print(res)

>['10.0.0.1 - GET 2020-08-24']

CodePudding user response:

Use multimode:

from statistics import multimode

lines = ["10.0.0.1 - GET 2020-08-24",
         "10.0.0.1 - GET 2020-08-24",
         "10.0.0.2 - GET 2020-08-20",
         "10.0.0.2 - GET 2020-08-20"]

ips = (line.split()[0] for line in lines)
print(','.join(sorted(multimode(ips))))
  • Related