Mary,

Mary,

It is often best to develop and test small parts of the project where you
can easily play with it, then move it into more complex configurations like
a function body

Here is your code:

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

I made a file on my local system and this works:

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

now you are returning an uppercase version of the current 'word" stored in
word. So what is the length of that word?

Here is the modified variation on your code:

>>> [word.upper() for line in open('TESTINK.txt', 'r') for word in
line.split()]
['THIS', 'IS', 'LINE', 'ONE', 'AND', 'THIS', 'IS', 'ANOTHER', 'LINE',
'JUST', 'TO', 'TEST', 'WITH.']

Here is yet another modification showing the length, instead:

>>> [len(word) for line in open('TESTINK.txt', 'r') for word in
line.split()]
[4, 2, 4, 3, 3, 4, 2, 7, 4, 4, 2, 4, 5]

By your rules, you want to only keep those words where "len(word) > 3"

So where in the list comprehension would you add this an if condition to get
this?

['THIS', 'LINE', 'THIS', 'ANOTHER', 'LINE', 'JUST', 'TEST', 'WITH.']

Since you read in all your data using the same function, you might even make
it take an optional value to cut at, defaulting with 3 or even 0.

-----Original Message-----
From: Tutor <tutor-bounces+avigross=verizon....@python.org> On Behalf Of
Mary Sauerland
Sent: Thursday, December 20, 2018 10:49 AM
To: tutor@python.org
Subject: [Tutor] Python

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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to