Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - Count number of occurrences of each number

Tags:

python

json

I have a long string of numbers separated by commas. I can search and count the number of occurrences of most numbers, or more accurately, 2 digit numbers.

IF I have a number sequences like: 1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2 and I want to count how many times the number 1 appears I should really get 5.

However, because it is counting the 1 in 10,11 and 12, I am getting 9.

Does anyone know how to make the below code match ONLY whole "strings"?

def mostfreq(numString):
    import json 
    maxNum=45
    count=1
    list={}
    while count <= maxNum:
        list[count] = 0
        count+=1
    #numString is the array with all the numbers in it
    count=1
    topTen = ""
    while count <= maxNum:
        list[count]=numString.count(str(count))
        topTen = topTen+json.dumps(
        {count: list[count]},
        sort_keys=True,
        indent=4)+","
        count+=1
    response_generator = ( "["+topTen[:-1]+"]" )
    return HttpResponse(response_generator)
like image 526
eoinzy Avatar asked Sep 16 '11 02:09

eoinzy


1 Answers

On 2.7+, just split and use the collections.Counter:

from collections import Counter
numstring = "1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2"
numcount = Counter(numstring.split(','))

or, Pre-2.7:

from collections import defaultdict
numstring = "1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2"
numcount = defaultdict(int)
for num in numstring.split(','):
    numcount[num] += 1

If you want to use count:

numstring = "1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2"
numlist = numstring.split(',')
numcount = dict((num, numlist.count(num)) for num in set(numlist))

but it's O(m*n) rather than O(n) because it iterates the list of numbers once for each unique number.

like image 193
agf Avatar answered Oct 12 '22 22:10

agf