用python写数字出现的次数_python – 计算每个数字的出现次数

我用逗号分隔了一长串数字.我可以搜索和计算大多数数字的出现次数,或者更准确地说是2位数字.

如果我有一个数字序列,如:

1,2,3,4,5,1,6,7,1,8,9,10,11,12,1,1,2

而且我想计算1号出现的次数我应该得到5次.

但是,因为它计算的是10,11和12中的1,所以我得到了9.

有谁知道如何使下面的代码只匹配整个“字符串”?

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)

    原文作者:太阳r
    原文地址: https://blog.csdn.net/weixin_42299150/article/details/112933054
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞