如何查找从 .csv 文件创建的列表中的单词频率

How to find the frequency of words in a list created from a .csv file

我正在尝试编写一个程序,首先读取输入文件的名称,然后使用 csv.reader() 方法读取文件。该文件包含以逗号分隔的单词列表。该程序应输出单词及其频率(每个单词在文件中出现的次数),没有任何重复。

文件 input1.csvhello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,猫,嘿,男孩

到目前为止我有这个:

import csv
with open('input1.csv', 'r') as wordsfile:
words_reader = csv.reader(wordsfile)
for row in words_reader:
    for word in row:
        count = row.count(word)
        print(word, count)

但我的输出是这样的: “你好 1 猫 2 男人 2 嘿 2 狗 2 男孩 2 你好 1 男人 2 猫 2 女人 1 狗 2 类别 1 嘿 2 男孩 2

我正在尝试输出此内容,但没有任何重复项,我很困惑,如有任何帮助,我们将不胜感激。

尝试使用 set()

import csv
with open('input1.csv', 'r') as wordsfile:
words_reader = csv.reader(wordsfile)
for row in words_reader:
    list_of_words = set(row)
    for word in list_of_words:
        count = row.count(word)
        print(word, count)

我对 csv 库不是很熟悉,我不知道 row 是否是一个列表,如果这会抛出错误,我很抱歉。 如果行是一个字符串可能你可以使用

row = row.split()
list_of_words = set(row)

希望对您有所帮助。

import csv

input1 = input()

with open(input1, 'r') as wordsfile:
    words_reader = csv.reader(wordsfile)
    for row in words_reader:
        list_of_words = row

no_duplicates_in_list = list(dict.fromkeys(list_of_words))
listlength = len(no_duplicates_in_list)

for i in range(listlength):
    print(no_duplicates_in_list[i], list_of_words.count(no_duplicates_in_list[i]))

与 Aryman 的几乎相同,但顺序与 csv 中的顺序相同

import csv

name = input()
with open(name, 'r') as myfile:
    Reader = csv.reader(myfile, delimiter=',')
    dictionary = dict()
    for l in Reader:
        for m in l:
            if m in dictionary:
                dictionary[m] = dictionary[m] + 1
            else:
                dictionary[m] = 1
    for n in list(dictionary.keys()):
        print("{} {}".format(n, dictionary[n]))

好吧,所以我对 Python 很基础,但我在尝试不同的 for 循环等大约一个小时后就弄明白了。我最终坚持使用列表,因为这就是说明中指示的分配。为了去除第一个列表中的重复项,我制作了第二个列表并嵌套了一个 if 语句,该语句仅添加其中不包含的单词,从而生成一个新列表,其中包含第一个单词的每个副本。


filename = input()
words = []
new_words = []
with open(filename, 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter = ',')
    for row in reader:
        for word in row:
            words.append(word)
        for word in words:
            freq = words.count(word)
            if word not in new_words:
                new_words.append(word)
                print(word, freq)
         

导入 csv 单词 = {} user_file = 输入() 打开(user_file,“r”)作为 csvfile: 输入阅读器 = csv.reader(csvfile) 对于输入阅读器中的行: 单词列表=行

    for i in row:
        if i in words:
            words[i] += 1
        else:
             words[i] = 1

我的话: 打印(我,单词[i])