Python Lists - Finding Number of Times a String Occurs

后端 未结 8 452
独厮守ぢ
独厮守ぢ 2020-12-10 17:21

How would I find how many times each string appears in my list?

Say I have the word:

\"General Store\"

that is in my list like 20 t

8条回答
  •  庸人自扰
    2020-12-10 17:33

    votes=["General Store", "Mall", "General Store","Ice Cream Van","General Store","Ice Cream Van","Ice Cream Van","General Store","Ice Cream Van"]
    
    for vote in set(votes):
        print(vote+" - voted "+str(votes.count(vote))+" times")
    

    Try this. It will output

    General Store - voted 4 times
    Ice Cream Van - voted 4 times
    Mall - voted 1 times
    

提交回复
热议问题