python计算列表内元素出现次数

result=['normal', 'normal', 'test2', 'test1', 'test2', 'test1', 'normal', 'test1', 'normal', 'test2', 'test2', 'test2', 'test1', 'test2', 'test1', 'test2', 'normal', 'test2', 'test2', 'normal', 'normal', 'normal', 'normal', 'test2', 'normal', 'test1', 'normal', 'normal', 'test1', 'test2', 'test2', 'normal', 'test2', 'normal', 'test2', 'test2', 'normal', 'normal', 'test1', 'test2', 'test1', 'test1', 'test1', 'normal', 'test2', 'test1', 'test2', 'test2', 'test2', 'normal', 'test1', 'test2', 'test1', 'normal', 'test2', 'test2', 'test1', 'test1', 'normal', 'test1', 'normal', 'test2', 'normal', 'normal', 'test1', 'normal', 'normal', 'test2', 'normal', 'test1', 'normal', 'normal', 'test1', 'test2', 'normal', 'test1', 'test2', 'test2', 'test1', 'test2', 'test1', 'test1', 'test2', 'normal', 'test2', 'test1', 'normal', 'test2', 'normal', 'normal', 'test2', 'test2', 'test1', 'normal', 'test2', 'normal', 'normal', 'test1', 'test1', 'test2']


def countX(lst, x):
count = 0
for ele in lst:
if (ele == x):
count = count + 1
return count


normal=countX(result,"normal")
test1=countX(result,"test1")
test2=countX(result,"test2")

print("normal:",normal)
print("test1:",test1)
print("test2:",test2)
posted @ 2020-06-30 20:20  正气的割草机  阅读(311)  评论(0)    收藏  举报