gpt4 book ai didi

Python 列表 - 查找字符串出现的次数

转载 作者:太空狗 更新时间:2023-10-30 00:20:47 24 4
gpt4 key购买 nike

我如何找到每个字符串在我的列表中出现的次数?

假设我有话:

"General Store"

这在我的列表中出现了 20 次。我如何才能知道它在我的列表中出现了 20 次?我需要知道这一点,这样我才能将该数字显示为一种"poll vote" answer。

例如:

General Store - voted 20 times
Mall - voted 50 times
Ice Cream Van - voted 2 times

我将如何以类似于此的方式显示它?:

General Store
20
Mall
50
Ice Cream Van
2

最佳答案

使用count 方法。例如:

(x, mylist.count(x)) for x in set(mylist)

关于Python 列表 - 查找字符串出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11800755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com