7.8实验室:词频(列表和CSV)-我如何读取CSV并消除重复?

igsr9ssn  于 2022-12-15  发布在  其他
关注(0)|答案(4)|浏览(103)

这是我的代码,但是我不知道如何删除重复的代码。我试过创建新的列表、dict、元组等。
编写一个程序,首先读取输入文件的名称,然后使用csv.reader()方法读取该文件。该文件包含一个由逗号分隔的单词列表。您的程序应该输出单词及其频率(每个单词在文件中出现的次数),并且没有任何重复。
例如:如果输入为:
input1.csv
input1.csv的内容为:
hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy
输出为:

hello 1
cat 2
man 2
hey 2
dog 2
boy 2
Hello 1
woman 1
Cat 1

注意:输出的末尾有一个换行符,input1.csv可供下载。

import csv
user_input = input() 
with open(user_input, 'r') as name_CSV: 
    paper_copy = csv.reader(name_CSV)
    for lines in paper_copy:
        for w in lines: 
            words_cnt = lines.count(w)
            print(w, words_cnt)
p5fdfcr1

p5fdfcr11#

一个非常复杂的方法是将所有单词添加到列表中,然后将列表转换为集合,这样就完成了
集合只允许一个文本示例,因此将字符串列表转换为集合{集合像字典一样位于大括号中}会立即删除所有重复项

import csv

word_list=[]
user_input = input() 
with open(user_input, 'r') as name_CSV: 
    paper_copy = csv.reader(name_CSV)
    for lines in paper_copy:
        for w in lines: 
            word_list.append(w)
word_set = set(word_list)
c3frrgcw

c3frrgcw2#

你可以使用一些方法来唯一化一个list,但是最好的方法是把它转换成一个set(在python中什么是集合?)
因此,首先在您的单词中加载csv.reader,就像您被告知的那样:

import csv

word_list = []
with open('input1.csv') as name_CSV:
    paper_copy = csv.reader(name_CSV)
    for line in paper_copy:
        for word in line:
            word_list.append(word)

然后将其转换为set。我对set进行了排序,以保持单词从word_list开始出现的顺序,因为set天生不保持任何顺序(根据定义,它们是无序的)。

unique_words = sorted(set(word_list), key=word_list.index)

然后,为了获得输出,for loop遍历set中的所有唯一单词,并将它们与list中的单词进行比较,同时每次在for loop中命中这些单词时,增加它们的计数:

for x in unique_words:
    count = 0
    for y in word_list:
        if x == y:
            count += 1
    print(x, count)

输出:

hello 1
cat 2
man 2
hey 2
dog 2
boy 2
Hello 1
woman 1
Cat 1

或者,您可以使用count()使用更少的行来完成它。我仍然认为至少看看上面的方法并尝试了解它是如何工作的是一个好主意。

for x in unique_words:
    print(x, word_list.count(x))

输出:

hello 1
cat 2
man 2
hey 2
dog 2
boy 2
Hello 1
woman 1
Cat 1
xkftehaa

xkftehaa3#

本实验有多种方法。您可以像其他人一样使用集合,因为集合的独特之处在于它们不允许重复单词。另一种方法是创建一个列表并使用count()功能:

# Needed for using csv.reader()
import csv

# The list used to check for duplicates
my_list = []

# You are taking input for the name of the csv file and only reading it.
with open(input(), 'r') as csvfile:
    # Reads the file and makes a list with whatever elements are delimited.
    word_reader = csv.reader(csvfile, delimiter = ',')
    
    # Obtains the row of words from word_reader.
    for row in word_reader:
        # Iterates through each word in the row.
        for word in row:
            # Adds each word to my_list.
            my_list.append(word)      
            
            # If only one instance of word is present in my_list:
            """ Statement does not execute if a duplicate word is added to
            my_list because then my_list.count(word) != 1 """ 
            if my_list.count(word) == 1:
                # Print the word followed by how many times it occurred in row.
                print("{} {}".format(word, row.count(word)))
3okqufwl

3okqufwl4#

import csv

word_list=[]
user_input = input() 
with open(user_input, 'r') as name_CSV: 
    paper_copy = csv.reader(name_CSV)
    for lines in paper_copy:
        for w in lines: 
            word_list.append(w)
word_set = set(word_list)
unique_words = sorted(set(word_list), key=word_list.index)
for x in unique_words:
    count = 0
    for y in word_list:
        if x == y:
            count += 1
    print(x, count)

相关问题