def count(squence,item):
count=0
i=0
for item in squence:
if item == squence:
count+=1
print count
count([1,7,8,7,7],7)
我不明白为什么 If 语句不起作用:( 谢谢,
最佳答案
list
已经有一个函数 count()
:[1,7,8,7,7].count(7)
返回3
但是你想做的是:
def count(squence, item):
cnt = 0
for i in squence:
if i == item:
cnt += 1
print cnt
在您的代码中,您覆盖了item
:item
是您想要计数的值,但它也是您检查的值,因此它不起作用...
关于 python |返回该项目在列表中出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732845/