[Tutor] Python Word Problems(Student)

Alan Gauld alan.gauld at yahoo.co.uk
Thu Oct 6 12:50:35 EDT 2016


On 06/10/16 15:15, Zeel Solanki wrote:

> def filter_long_words(words, n):
>   for x in words.split():
>     return filter(lambda x: len(x) > n, words)

You are trying to be too clever here, you don't need
the loop.

words is the original string so your filter will
only receive the full string each time. But you
can remove the loop and just use:

 return filter(lambda x: len(x) > n, words.split())

Bob has already addressed your problem of reading
n from raw_input().

BTW
Nowadays this would more commonly be done using a list
comprehension rather than reduce. I don't know if you
have covered comprehensions yet?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list