[英]How to count the number of times a word appears in each row of a csv file, in python
I have a csv document in which each row contains a string of words.我有一个 csv 文档,其中每一行都包含一串单词。 For each row, I want to know how many times each words appears in that row, how would I go about this?
对于每一行,我想知道该行中每个单词出现多少次,我该怎么做?
Thanks谢谢
Move the row to a list, make it unique and count every object in list,将行移动到列表中,使其唯一并计算列表中的每个对象,
For converting to list Check this转换为列表检查这个
if result is more than one, print it.如果结果不止一个,则打印它。
Considered this as list ['a','b','a','c']
将此视为列表
['a','b','a','c']
z=['a','b','a','c']
u=set(z)
for i in u:
if z.count(i)>1: print i,z.count(i)
Output:输出:
`a 2`
Use collections.Counter
:使用
collections.Counter
:
from collections import Counter
z = ['a', 'b', 'c', 'd', 'a']
u = Counter(z)
print(u['a']) # 2
print(u['b']) # 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.