Reputation:
I have an input file with sentences like this:
I like apples
My mother is called Anna.
I transfer these sentences to a list and then I want to remove words that have the length < 3.
I've tried this:
with open("fis.txt", "r", encoding="utf8") as f:
lst = [w.lower() for w in f.readlines() if len(w) >= 3]
print(lst)
but it gives me ['i like apples', 'my mother is called anna.']
and I want to obtain ['like apples', 'mother called anna.']
What seems to be the problem here?
Upvotes: 1
Views: 79
Reputation: 11
It is taking the entire sentence and not individual words, try iterating through w and then check for length.
Upvotes: 0
Reputation: 82765
Try:
with open("fis.txt", "r", encoding="utf8") as f:
print( [" ".join(j for j in w.split() if len(j) >= 3 ) for w in f.readlines() ] )
Output:
['like apples', 'mother called Anna.']
Upvotes: 2
Reputation: 78690
f.readlines()
gives you a list with two items which correspond to the two lines of the file.
You need to iterate over the lines (no need to read them into memory first, iterating over f
will do), split each line, and then filter the words.
with open("fis.txt", "r", encoding="utf8") as f:
lst = [' '.join(w.lower() for w in line.split() if len(w) >= 3) for line in f]
Upvotes: 3