Home > database >  Word frequencies from CSV List
Word frequencies from CSV List

Time:04-15

How do you write a list that only counts distinct words from a csv list?

Input: input1.csv input1.csv = hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy

Expected Output:
hello 1
cat 2
man 2
hey 2
dog 2
boy 2
Hello 1
woman 1
Cat 1

CodePudding user response:

import collections

with open(fname,"r") as f:
     word_counter = collections.Counter(f.read().split(","))
     print(word_counter.most_common())

Is probably what I would do ...

CodePudding user response:

First off the files being input can and do vary, so the input() is important for different filenames. Secondly we have to make a distinct/unique list so that we have something to work with, establishing the unique = [] is important to put your list somewhere. Lastly, two loops are needed for this scenario. This can be built differently but, I found this way to be the easiest to write and understand the code.

unique_list = []
with open(input(), 'r') as csvfile:
    csv_reader = csv.reader(csvfile, delimiter=',')
    for row in csv_reader:
        for word in row:
            if word not in unique_list:
                unique_list.append(word)
        for unique_word in unique_list:
            count = row.count(unique_word)
            print(unique_word, count)
  • Related