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)
On 2.7+, just
splitand use thecollections.Counter:or, Pre-2.7:
If you want to use
count:but it’s O(m*n) rather than O(n) because it iterates the list of numbers once for each unique number.