Op 06-04-15 om 16:05 schreef Jim Mooney:
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]
Your problem is in the above line. Have this example:
>>> words = ["foo", "bar", "spam", "eggs"]
>>> k = [word.rstrip for word in words]
>>> k
[<built-in method rstrip of str object at 0x7fd1f3cb1538>, <built-in
method rstrip of str object at 0x7fd1f3cb1880>, <built-in method rstrip
of str object at 0x7fd1f3cb1148>, <built-in method rstrip of str object
at 0x7fd1f3cda3e8>]
Looks very similar to your TypeError later on!
Let's see what's wrong:
>>> "foo".rstrip
<built-in method rstrip of str object at 0x7fd1f3cb1538>
Still the same...
Let's go further:
>>> "foo".rstrip()
'foo'
Looks much better!
So the problem is you're storing the rstrip function instead of calling
it and store the result. A big difference.
The fix is very easy, just call rstrip in your list comprehension:
k = [word.rstrip() for word in words]
A little tip: errors like these are easy to debug by printing your
values. If you printed k before continuing it wouldn't have happened.
Timo
len(k)
61406
p = filter(lambda word: len(word) > 10, k)
p
<filter object at 0x01992F30>
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()
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor