I have a file with different IP's.
192.168.11.2
192.1268.11.3
192.168.11.3
192.168.11.3
192.168.11.2
192.168.11.5
This is my code until now. Where I print the IP and the occurence, but how can I found out when the last occurennce was for each of the IP's. Is it a simple way to do so?
liste = []
dit = {}
file = open('ip.txt','r')
file = file.readlines()
for line in file:
liste.append(line.strip())
for element in liste:
if element in dit:
dit[element] +=1
else:
dit[element] = 1
for key,value in dit.items():
print "%s occurs %s times, last occurence at line" %(key,value)
Output:
192.1268.11.3 occurs 1 times, last occurence at line
192.168.11.3 occurs 2 times, last occurence at line
192.168.11.2 occurs 2 times, last occurence at line
192.168.11.5 occurs 1 times, last occurence at line