Python - counting the number of occurrences of each number

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

IF I have numerical sequences such as: 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, since it counts 1 in 10 , 11 and 12 , I get 9 .

Does anyone know how to make the code below 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) 
+8
json python
source share
1 answer

In 2.7+, just split and use 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 O (m * n), not O (n), as it iterates over the list of numbers once for each unique number.

+8
source share

All Articles