[Tutor] Python

Mary Sauerland msauerland815 at gmail.com
Thu Dec 20 10:49:25 EST 2018


Hi, 

I want to get rid of words that are less than three characters but I keep getting errors. I tried multiple ways but keep getting errors. 

Here is my code:

f1_name = "/Users/marysauerland/Documents/file1.txt"
#the opinions
f2_name = "/Users/marysauerland/Documents/file2.txt"
#the constitution


def read_words(words_file):
    return [word.upper() for line in open(words_file, 'r') for word in line.split()]


read_words(f1_name)
#performs the function on the file
set1 = set(read_words(f1_name))
#makes each word into a set and removes duplicate words
read_words(f2_name)
set2 = set(read_words(f2_name))

count_same_words = 0

for word in set1:
    if word in set2:
        count_same_words += 1
#comparing the set1 (set of unique words in the opinions) with set2 (set of unique words in the constitution) and adding 1 for each matching word found which is just counting the words
print(count_same_words)


Best, 

Mary


More information about the Tutor mailing list