[Tutor] failed filter result

Alan Gauld alan.gauld at btinternet.com
Mon Apr 6 20:12:58 CEST 2015


On 06/04/15 15:05, Jim Mooney wrote:
> Why did this fail where it did? It failed at listing the result of the
> filter of a word list, but I figured if it failed, it would have done so at
> the filter.
>
>>>> words = open('5desk.txt').readlines()
>>>> k = [word.rstrip for word in words]

Notice that you are not calling rstrip here. You are creating a list 
with as many references to word.rstrip as there are words. This is 
almost certainly not what you want. You need to add the parens.

>>>> p = filter(lambda word: len(word) > 10, k)
>>>> list(p)
> Traceback (most recent call last):
>    File "<string>", line 301, in runcode
>    File "<interactive input>", line 1, in <module>
>    File "<interactive input>", line 1, in <lambda>
> TypeError: object of type 'builtin_function_or_method' has no len()

This is because you are passing a list of function
references rather than stripped words.


-- 
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