I have a my_list which looks like this:
[['root', '151.62.163.222'], ['tergul', '151.62.163.222'], ['root', '201.179.14.4'], ['root', '201.179.14.4'], ['admin', '201.179.14.4'], ['admin', '201.179.14.4'], ['lemming', '10.0.0.202'], ['lemming', '10.0.0.202'], ['lemming', '10.0.0.202'], ['lemming', '10.0.0.202'], ['lemming', '10.0.0.202'], ['lemming', '10.0.0.202']]
Now, I want to count the number of unique IP Adresses and stack them together with their counts. The result should be a list and look like this:
[['151.62.163.222', '2'],['201.179.14.4', '4'],['10.0.0.202', '6']
I have code that does this with numpy arrays:
unq, count = np.unique(my_array[:,1], axis=0, return_counts=True)
failed_per_IP = np.column_stack((unq, count))
I cannot use numpy as it is not installed on the server I am running it on. How would I do this using Python standard libraries and only lists?
Counter is good but if you are interested in a more in depth implementation you can use a dictionary and keep track of the counts as you traverse the IPs.
This is a good exercise but a cleaner approach definitely uses a counter.