1

I learning Python and during solution an exercise, function filter() returns empty list and i can't understand why. Here is my source code:

"""
Using the higher order function filter(), define a function filter_long_words()
that takes a list of words and an integer n and returns
the list of words that are longer than n.
"""

def filter_long_words(input_list, n):
    print 'n = ', n
    lengths = map(len, input_list)
    print 'lengths = ', lengths
    dictionary = dict(zip(lengths, input_list))
    filtered_lengths = filter(lambda x: x > n, lengths) #i think error is here
    print 'filtered_lengths = ', filtered_lengths
    print 'dict = ',dictionary
    result = [dictionary[i] for i in filtered_lengths]
    return result

input_string = raw_input("Enter a list of words\n")
input_list = []
input_list = input_string.split(' ')
n = raw_input("Display words, that longer than...\n")

print filter_long_words(input_list, n)
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Absolut
  • 1,220
  • 1
  • 9
  • 11
  • Python is not lisp (wouldn't write the code like that there either though), if you don't get extra points for coming up with the most obscure possible solution to problems, go with the simple, idiomatic way: `[word for word in input_list if len(word) > n]` is much cleaner and easier to understand no? – Voo Sep 01 '13 at 11:24
  • @Voo: Yes, but the excercise does say he say to use `filter()`. – Lennart Regebro Sep 01 '13 at 11:25
  • 1
    @Lennart Point taken, homework may be silly but professors still prefer if you do it just as they say ;) Still tragic to teach people python that way. – Voo Sep 01 '13 at 11:29

4 Answers4

4

Your function filter_long_words works fine, but the error stems from the fact that when you do:

n = raw_input("Display words, that longer than...\n")
print filter_long_words(input_list, n)  

n is a string, not an integer.

Unfortunately, a string is always "greater" than an integer in Python (but you shouldn't compare them anyway!):

>>> 2 > '0'
False

If you're curious why, this question has the answer: How does Python compare string and int?


Regarding the rest of your code, you should not create a dictionary that maps the lengths of the strings to the strings themselves.

What happens when you have two strings of equal length? You should map the other way around: strings to their length.

But better yet: you don't even need to create a dictionary:

filtered_words = filter(lambda: len(word) > n, words)
Community
  • 1
  • 1
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
1

n is a string. Convert it to an int before using it:

n = int(raw_input("Display words, that longer than...\n"))

Python 2.x will attempt to produce a consistent-but-arbitrary ordering for objects with no meaningful ordering relationship to make sorting easier. This was deemed a mistake and changed in the backwards-incompatible 3.x releases; in 3.x, this would have raised a TypeError.

user2357112
  • 260,549
  • 28
  • 431
  • 505
0

I don't know what your function does, or what you think it does, just looking at it gives me a headache.

Here's a correct answer to your exercise:

def filter_long_words(input_list, n):
    return filter(lambda s: len(s) > n, input_list)
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
0

My answer:

def filter_long_words():
     a = raw_input("Please give a list of word's and a number: ").split()
     print "You word's without your Number...", filter(lambda x: x != a, a)[:-1]

filter_long_words()    
iratxe
  • 87
  • 9