user9886692
user9886692

Reputation:

Remove words form list sentences that have length < 3

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

Answers (3)

Arth Dh
Arth Dh

Reputation: 11

It is taking the entire sentence and not individual words, try iterating through w and then check for length.

Upvotes: 0

Rakesh
Rakesh

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

timgeb
timgeb

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

Related Questions