Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display list items and count occurrences from list

Hi this is my first time posting here so apologies for format etc.

So i am doing a Python programming course at college just now and it’s assessment time.

The assessment is to take an already made logfile.txt that has iIP addresses in it on separate lines and we have to make a program that analyses and displays the ip addresses and also the count of how many times it appears and then separately the same list but display the most common IP occurrence.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So far I have managed to open the file and split it so it takes the IP addresses and add them to a list and a function that displays the most common IP, however, when I try display the full list of addresses and how many times each one appeared I just cannot get it to work.

I have tried using collections.Counter but i keep getting a "non hashable error"

I’m not sure where to go from here as no other way of doing a list is working and taking the IP addresses and manually putting them in a list inside Python is not allowed.

here is my code so far:

filename = input("Enter the name of the file: ")
readfile = open(filename, 'r')

def most_frequent(my_list2):
    counter = 0
    num = my_list2 [0]

    for i in my_list2:
        curr_frequency = my_list2.count(i)
        if(curr_frequency> counter):
            counter = curr_frequency
            num = i
    return num

my_list2 = []
for word in readfile:
    my_list2.append(word.split(None)[:1])
#print(my_list)

print("The most common IP Address is:", (most_frequent(my_list2)))

result

The most common IP Address is: ['172.16.17.108']

my_list2

[['172.16.121.170'], ['172.16.17.54'], ['172.16.17.108'], ['172.16.17.41'], ['172.16.121.115'], ['172.16.121.97'], ['172.16.121.38'], ['172.16.121.38'], ['172.16.121.97'], ['172.16.121.35'], ['172.16.121.104'], ['172.16.17.56'], ['172.16.121.53'], ['172.16.17.108'], ['172.16.17.56'], ['172.16.17.191'], ['172.16.31.213'], ['172.16.17.191'], ['172.16.121.170'], ['172.16.121.226'], ['172.16.17.108'], ['172.16.17.56'], ['172.16.121.38'], ['172.16.121.171'], ['172.16.121.171'], ['172.16.121.64'], ['172.16.17.56'], ['172.16.17.191'], ['172.16.17.41'], ['172.16.121.35'], ['172.16.17.108'], ['172.16.121.121'], ['172.16.121.226'], ['172.16.121.38'], ['172.16.121.104'], ['172.16.17.108'], ['172.16.121.35'], ['172.16.121.240'], ['172.16.17.54'], ['172.16.31.214'], ['172.16.121.240'], ['172.16.121.240'], ['172.16.121.170'], ['172.16.31.128'], ['172.16.31.214'], ['172.16.17.54'], ['172.16.121.104'], ['172.16.121.171'], ['172.16.17.56'], ['172.16.121.170'], ['172.16.17.41'], ['172.16.121.121'], ['172.16.121.35'], ['172.16.121.38'], ['172.16.121.115'], ['172.16.31.128'], ['172.16.121.115'], ['172.16.17.108'], ['172.16.31.213'], ['172.16.121.53'], ['172.16.31.213'], ['172.16.121.171'], ['172.16.121.53'], ['172.16.121.240'], ['172.16.17.41'], ['172.16.17.108'], ['172.16.17.41'], ['172.16.121.35'], ['172.16.17.191'], ['172.16.121.226'], ['172.16.121.53'], ['172.16.17.191'], ['172.16.121.240'], ['172.16.17.191'], ['172.16.17.41'], ['172.16.121.35'], ['172.16.121.35'], ['172.16.121.171'], ['172.16.121.121'], ['172.16.121.38'], ['172.16.17.108'], ['172.16.17.191'], ['172.16.121.53'], ['172.16.121.53'], ['172.16.17.191'], ['172.16.121.53'], ['172.16.121.35'], ['172.16.121.53'], ['172.16.121.97'], ['172.16.121.121'], ['172.16.121.35'], ['172.16.121.104'], ['172.16.121.226'], ['172.16.17.108'], ['172.16.121.53'], ['172.16.17.191'], ['172.16.121.115'], ['172.16.121.170'], ['172.16.121.171'], ['172.16.31.128'], ['172.16.121.121'], ['172.16.17.54'], ['172.16.121.121'], ['172.16.17.56'], ['172.16.121.121'], ['172.16.17.108'], ['172.16.121.38'], ['172.16.121.240'], ['172.16.17.191'], ['172.16.121.64'], ['172.16.121.171'], ['172.16.121.170'], ['172.16.121.121'], ['172.16.121.35'], ['172.16.121.64'], ['172.16.121.104'], ['172.16.121.64'], ['172.16.17.108'], ['172.16.17.108'], ['172.16.121.97'], ['172.16.31.213'], ['172.16.121.171'], ['172.16.121.38'], ['172.16.121.226'], ['172.16.121.226'], ['172.16.17.108'], ['172.16.121.35'], ['172.16.121.35'], ['172.16.121.240'], ['172.16.31.213'], ['172.16.121.171'], ['172.16.121.121'], ['172.16.17.191'], ['172.16.121.38'], ['172.16.121.64'], ['172.16.17.41'], ['172.16.31.213'], ['172.16.17.41'], ['172.16.17.54'], ['172.16.17.56'], ['172.16.17.54'], ['172.16.121.104'], ['172.16.121.35'], ['172.16.121.97'], ['172.16.121.226'], ['172.16.17.54'], ['172.16.121.115'], ['172.16.17.56'], ['172.16.121.240'], ['172.16.31.128'], ['172.16.17.41'], ['172.16.17.191'], ['172.16.121.121'], ['172.16.17.191'], ['172.16.17.56'], ['172.16.31.214'], ['172.16.17.108'], ['172.16.121.64'], ['172.16.121.53'], ['172.16.17.41'], ['172.16.17.54'], ['172.16.17.56'], ['172.16.121.64'], ['172.16.121.226'], ['172.16.17.54'], ['172.16.121.115'], ['172.16.31.214'], ['172.16.17.108'], ['172.16.121.35'], ['172.16.17.54'], ['172.16.31.214'], ['172.16.121.240'], ['172.16.17.191'], ['172.16.121.171'], ['172.16.17.41'], ['172.16.121.171'], ['172.16.121.121'], ['172.16.17.54'], ['172.16.121.104'], ['172.16.121.171'], ['172.16.121.121'], ['172.16.121.121'], ['172.16.121.170'], ['172.16.121.64'], ['172.16.121.97'], ['172.16.121.170'], ['172.16.121.240'], ['172.16.121.64'], ['172.16.31.128'], ['172.16.121.115'], ['172.16.17.56'], ['172.16.121.240'], ['172.16.121.64'], ['172.16.121.226'], ['172.16.121.64'], ['172.16.121.64'], ['172.16.121.64'], ['172.16.121.64'], ['172.16.121.35'], ['172.16.121.240']]

any help is appreciated thanks

>Solution :

list are not hashable, tuple, string, int are.

you need to change code to get the items as list of ip adress ie [<ip add1>, <ip add2>,...] not as list of list of ip address ie [[<ip add 1], [<ip add 2>]..]

ie you need to change my_list2 from

[['172.16.121.170'], ['172.16.17.54'], ['172.16.17.108'],...]

to

['172.16.121.170', '172.16.17.54', '172.16.17.108',...]

so changes you need to make

my_list2 = []
for word in readfile:
    my_list2.extend(word.split(None)[:1])

then you can use collections.Counter to get the ip address with there frequency

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading