Python列表-查找字符串出现的次数
如何查找每个字符串在列表中出现的次数 说我有这个词:Python列表-查找字符串出现的次数,python,Python,如何查找每个字符串在列表中出现的次数 说我有这个词: "General Store" 这在我的清单上大概有20次。我怎样才能发现它在我的列表中出现了20次?我需要知道这一点,以便将该数字显示为一种类型的“投票”答案 例如: 我将如何以类似于此的方式显示它 General Store 20 Mall 50 Ice Cream Van 2 使用count方法。例如: (x, mylist.count(x)) for x in set(mylist) 举个简单的例子: >>&g
"General Store"
这在我的清单上大概有20次。我怎样才能发现它在我的列表中出现了20次?我需要知道这一点,以便将该数字显示为一种类型的“投票”
答案
例如:
我将如何以类似于此的方式显示它
General Store
20
Mall
50
Ice Cream Van
2
使用
count
方法。例如:
(x, mylist.count(x)) for x in set(mylist)
举个简单的例子:
>>> lis=["General Store","General Store","General Store","Mall","Mall","Mall","Mall","Mall","Mall","Ice Cream Van","Ice Cream Van"]
>>> for x in set(lis):
print "{0}\n{1}".format(x,lis.count(x))
Mall
6
Ice Cream Van
2
General Store
3
首先使用set()获取列表中所有唯一的元素。然后在集合上循环以从列表中计数元素
unique = set(votes)
for item in unique:
print item
print votes.count(item)
虽然其他答案(使用list.count)确实有效,但它们在大型列表上的速度可能会非常慢
考虑使用集合。计数器,如中所述
例如:
>>> # Tally occurrences of words in a list
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
... cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})
我喜欢这样的问题的单线解决方案:
def tally_votes(l):
return map(lambda x: (x, len(filter(lambda y: y==x, l))), set(l))
你可以使用字典,你可能想考虑从一开始就使用一个字典而不是一个列表,但是这里有一个简单的设置。
#list
mylist = ['General Store','Mall','Ice Cream Van','General Store']
#takes values from list and create a dictionary with the list value as a key and
#the number of times it is repeated as their values
def votes(mylist):
d = dict()
for value in mylist:
if value not in d:
d[value] = 1
else:
d[value] +=1
return d
#prints the keys and their vaules
def print_votes(dic):
for c in dic:
print c +' - voted', dic[c], 'times'
#function call
print_votes(votes(mylist))
它输出:
Mall - voted 1 times
Ice Cream Van - voted 1 times
General Store - voted 2 times
试试这个。它将输出
General Store - voted 4 times
Ice Cream Van - voted 4 times
Mall - voted 1 times
如果你愿意使用熊猫图书馆,这一个真的很快:
import pandas as pd
my_list = lis=["General Store","General Store","General Store","Mall","Mall","Mall","Mall","Mall","Mall","Ice Cream Van"]
pd.Series(my_list).value_counts()
你想让它区分大小写吗?我怀疑这很重要,因为我可以先打印字母,然后使用for循环,计算出现的次数,然后把它们放在那里。但是,是的,我希望它们区分大小写。:)这似乎是我正在寻找的,我理解循环,但我不知道关于set函数。我试着观看,但我仍然不明白我在设置什么。也许你能解释一下投票代表什么?就像我的意思,我会把“杂货店”放在选票上吗?但很明显,这应该放在“item”中,您可能会因为一个原因而将其区分开来。“投票”是您的“列表”,其中包含您想要计数的重复字符串。set()不设置任何内容,它通过删除重复项将列表转换为集合。哦,好的。我现在已经让它工作了,但是当我试着测试它时,当我使用代码时会出现类似的情况:粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0粮食产品0水果和蔬菜0水果和蔬菜0水果和蔬菜0水果和蔬菜0水果蔬菜0水果和蔬菜0水果和蔬菜0我如何使它成为一个单一的类别,在一个地方得到所有计数的总和,而且,如果用户在那一刻点击不同的单选按钮,计数变为0。.data文件包含了所有的选项,但它并没有保留以前的计数。看起来代码中还有很多未在这里描述的内容。您是否有一个列表/数组对象,其中包含每次投票的所有字符串?为什么不只使用
cnt=Counter(['red'、'blue'、'red'、'green'、'blue'、'blue'])
?实际上,您还可以使用cnt=defaultdict(int)
——我想这在python 2.5中提供了一种O(N)方式来实现这一点。。。(与引入计数器时的2.7+相反)我只是从链接的collections doc页面复制了这个示例,但是是的,计数器([iterable])
构造函数绝对更干净、更简单!我可以想象list.count(x)
比len(filter(…))
更有效。
General Store - voted 4 times
Ice Cream Van - voted 4 times
Mall - voted 1 times
import pandas as pd
my_list = lis=["General Store","General Store","General Store","Mall","Mall","Mall","Mall","Mall","Mall","Ice Cream Van"]
pd.Series(my_list).value_counts()