bhat557
bhat557

Reputation: 105

Altering dictionaries/keys in Python

I have ran the code below in Python to generate a list of words and their count from a text file. How would I go about filtering out words from my "frequency_list" variable that only have a count of 1?

In addition, how would I export the print statement loop at the bottom to a CSV

Thanks in advance for any help provided.

import re
import string
frequency = {}
document_text = open('Words.txt', 'r')
text_string = document_text.read().lower()
match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string)

for word in match_pattern:
    count = frequency.get(word,0)
    frequency[word] = count + 1

frequency_list = frequency.keys()



for words in frequency_list:
    print (words, frequency[words])

Upvotes: 1

Views: 78

Answers (2)

coder
coder

Reputation: 12992

To filter out words, an alternative way would be:

frequency = dict(filter(lambda (k,v): v>1, frequency.items())) 

To export the print statement loop at the bottom to a CSV, you could do that:

import csv    
frequency_list = ['word1','word2','word3']  # example

with open('output.csv','w') as csvfile:
    writer = csv.writer(csvfile, delimiter=",")
    writer.writerow(frequency_list)

This will generate an 'output.csv' file with the words from your frequency_list in one row.

To get a row for each word try, the following:

with open('output.csv','w') as csvfile:
    writer = csv.writer(csvfile, delimiter=",")
    writer.writerows([i.strip() for i in l.split(',')] for l in frequency_list)

Update

To get a csv with the counters, keep your initial dictionary and do the following:

frequency = {"one":1,"two":2,"three":3} #example
with open('output.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    for key, value in frequency.items():
       writer.writerow([key, value])

Upvotes: 1

Tzahi T
Tzahi T

Reputation: 356

For the first part - you can use dict comprehension:

frequency = {k:v for k,v in frequency.items() if v>1}

Upvotes: 1

Related Questions