JasonOrtiz
JasonOrtiz

Reputation: 119

Sort dictionary based on count

I have the following code that works great. It get my IP address out of a file and counts how many times they appear in the logfile.

def count_ips():
    fp=open('logfile','r')
    store=[]
    while 1:
            line=fp.readline()
            if not line:
                    break
            if line[-1:] == '\n':
                    line=line[:-1]
            data1=line.split('"')
            data2=data1[0].split(' ')
            store.append({'IP':data2[0],'Date':data2[3]+' '+data2[4],'Action':' '.join(data1[1:-2]),'Browser':' '.join(data1[-2:])})
    fp.close()
    count={}
    for i in store:
            if i['IP'] in count:
                    count[i['IP']] +=1
            else:
                    count[i['IP']] =1

    avg=0
    cnt=0
    for i in count:
            avg+=count[i]
            cnt+=1
    avg=avg/cnt
    print 'average hit is: %i' % avg

    for i in count:
           if count[i] > 10:
                   print i +' %i' % count[i]
count_ips()

I dont really know how I got to this point but in this section. I would like to sort by the count before I print it out. Biggest number on the bottom.

    for i in count:
           if count[i] > 10:
                   print i +' %i' % count[i]

I feel at this point im just looking at things wrong and dont see the easy fix for my little dilemma.

Thank you for you help! Jason

Upvotes: 1

Views: 5995

Answers (3)

seeiespi
seeiespi

Reputation: 3828

Whenever I have to treat dictionaries as data I use pandas.

import pandas as pd
pd.DataFrame(list(dict.items()), columns= ['IP','count']).sort_values('count')

Notice that the items from the dictionary are called with dict.items() and then passed to a list. If using python 2.X then you should omit the list() call.

Upvotes: 0

Conan Li
Conan Li

Reputation: 494

so assume that you have a dictionary d which contain keys that are IPs and values are the counts.

>>> d = {'1.1.1.1':5, '2.2.2.2':4}

Here is what I would do in a one liner:

>>> sorted((d[ip], ip) for ip in d)
[(4, '2.2.2.2'), (5, '1.1.1.1')]

You can also use parameter reverse=True to sorted the list in reversed order.

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142146

Assuming that count is your dict of IP->Count, then:

from operator import itemgetter
sorted_counts = sorted(count.iteritems(), key=itemgetter(1))
for ip, cnt in sorted_counts:
    print ip, 'had', cnt, 'results'

Upvotes: 4

Related Questions