简体   繁体   English

如何计算一个单词出现在 csv 文件的每一行中的次数,在 python 中

[英]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.

相关问题 如何计算每个单词在 txt 文件中出现的次数? - How can I count the number of times each word appears in a txt file? 如何使用python计算字母在单词中出现的次数 - How count the number of times a letter appears in word using python 如何使用 python 计算特定字段的单词出现次数? - How to count the number of times a word appears for specific fields using python? 如何计算一个数字出现在文件中每个数字开头的次数? (蟒蛇) - How to count the number of times a digit appears at the beginning of each number in a file? (python) 如何计算一个单词序列在文件中出现的次数,使用Python中的MapReduce? - How to count the number of times a word sequence appears in a file, using MapReduce in Python? 计算每个成绩在文件中出现的次数 - Count Number of times each grade appears in a file 计算使用python使用文本文件中每个单词的次数 - count number of times each word in text file are used using python 使用字典计算每个字母在单词中出现的次数 - count the number of times each alphabet appears in a word using dictionary 计算每个名称在 CSV 文件和 plot 中出现的频率(Python) - Count how often each name appears in CSV file and plot it (Python) 计算每个符号出现在字符串中的次数 - python - Count the number of times each of the symbols appears in a string - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM