Re: [Tutor] Difference between filter and map
vanam wrote: Yes i did a mistake in expressing my problem below are the instances of the script and its corresponding output,for each instance its giving contrasting result i want explanation for that This has pretty much been explained already. Do you have some question with the explanation? [1]:def squ(n): return n*n filter(squ,range(3))output is not seen on the interpreter map(squ,range(3))-output not seen on the interpreter print filter(squ,range(3))-output is [1,2] print map(squ,range(3))--output is [0,1,4] [2]:def squ(n): y = n*n print y filter(squ,range(3))--Below is the output Note that in a script, the results of function calls are not printed unless you explicitly ask for it with a print statement. So the output here is from the print y in squ(), it is not the result of filter(). 0 1 4 map(squ,range(3))--Below is the output 0 1 4 Again, this is just the output from print y print filter(squ,range(3))---Below is the output 0 1 4 This is the result from filter(): [] print map(squ,range(3))--Below is the output 0 1 4 This is the result from map(): [None,None,None] I want to know why in each case its giving different results and diff between filter and map Please reread the previous replies, all of this is explained. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam [EMAIL PROTECTED] wrote Yes i did a mistake in expressing my problem below are the instances of the script and its corresponding output,for each instance its giving contrasting result i want explanation for that [1]:def squ(n): return n*n filter(squ,range(3))output is not seen on the interpreter map(squ,range(3))-output not seen on the interpreter I don;t know why you aren't seeing the output, I would expect more or less the same output as the next two lines. What tool are you using? Is it IDLE or something else? print filter(squ,range(3))-output is [1,2] print map(squ,range(3))--output is [0,1,4] The first one returns the actual items from the list where squ(item) evaluates to Ture, ie non zero. The first item (0) squared is zero so it is not included - it is filtered out, leaving 1 and 2 as the only output. The second one returns the result of passing each item through squ()each item. So you get 0,1,4 which are the squares of 0,1,2 [2]:def squ(n): y = n*n print y This function prrints the values then returns None (Python's default return value) which is equivalent to False in a boolean context. filter(squ,range(3))--Below is the output 0 1 4 filter applies squ to each item. squ prints the square. filter then returns an empty list since squ always returns None, which is false, so all items are filtered out. Why it is not printed by the interpreter I don't know. map(squ,range(3))--Below is the output 0 1 4 As above but this time map returns a list of 3 Nones. Why it is not printed I don't know. print filter(squ,range(3))---Below is the output 0 1 4 [] print map(squ,range(3))--Below is the output 0 1 4 [None,None,None] Exactly as above except the output from the functions is actually being printed. I want to know why in each case its giving different results and diff between filter and map Hopefully my explanation has made that clear. Why the versions without print do not display the result of the functions is a mystery. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: i want to know the difference between filter(function,sequence) and map(function,sequence).I tried for a simple script with an function which finds the square of the number,after including separately filter and map in the script i am getting the same results for instance def squ(x): return x*x filter(squ,range(1,3))-1,4(output) map(squ,range(1,3)-1,4(output) Are you sure about that? I get In [1]: def sq(x): return x*x ...: In [2]: filter(sq, range(3)) Out[2]: [1, 2] In [3]: map(sq, range(3)) Out[3]: [0, 1, 4] map(fn, lst) returns a new list with fn applied to each element of lst. In terms of list comprehensions, it is [ fn(x) for x in lst ]. filter(fn, lst) returns a new list containing all elements of the original list for which fn(x) is true. As a list comprehension, it is [ x for x in lst if fn(x) ] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
ya i am sure about that i am using python editor which has python intrepreter attached to it i got the same output for both filter and map def squ(n): y = n*n print y filter(y,range(3))-0 1 4 map(y,range(3))-0 1 4 On 1/23/07, Kent Johnson [EMAIL PROTECTED] wrote: vanam wrote: i want to know the difference between filter(function,sequence) and map(function,sequence).I tried for a simple script with an function which finds the square of the number,after including separately filter and map in the script i am getting the same results for instance def squ(x): return x*x filter(squ,range(1,3))-1,4(output) map(squ,range(1,3)-1,4(output) Are you sure about that? I get In [1]: def sq(x): return x*x ...: In [2]: filter(sq, range(3)) Out[2]: [1, 2] In [3]: map(sq, range(3)) Out[3]: [0, 1, 4] map(fn, lst) returns a new list with fn applied to each element of lst. In terms of list comprehensions, it is [ fn(x) for x in lst ]. filter(fn, lst) returns a new list containing all elements of the original list for which fn(x) is true. As a list comprehension, it is [ x for x in lst if fn(x) ] Kent -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
On Tue, 23 Jan 2007, vanam wrote: i want to know the difference between filter(function,sequence) and map(function,sequence). Hi Vanam, They may both take functions as input, but the intention of the functions is different. In the case of filter(), the input function is used to cull the good elements of the sequence out. def starts_with_ab(word): ... return word[:2] == 'ab' ... filter(starts_with_ab, [abracadabra, open sesame, abraham lincoln] ... ) ['abracadabra', 'abraham lincoln'] i am getting the same results for instance def squ(x): return x*x filter(squ,range(1,3))-1,4(output) map(squ,range(1,3)-1,4(output) This doesn't look right. Please copy and paste exactly what you typed, and exactly what the program outputted. You need to do this carefully or we can't reproduce what you see. In fact, concretely, when you reply to Kent, you show a different definition of squ(): ### def squ(n): y = n*n print y ### Be more careful next time. In this case, squ() isn't even returning back a value, so you can't expect good things to come out of filter() and map(): filter and map depend on the input function actually giving values back. What you're seeing on screen is the side-effects of calling squ() on every element. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: ya i am sure about that i am using python editor which has python intrepreter attached to it i got the same output for both filter and map def squ(n): y = n*n print y filter(y,range(3))-0 1 4 map(y,range(3))-0 1 4 This is quite different that what you posted the first time. This function squ() *prints* n*n but *returns* None (since it has no explicit return statement). The previous squ() actually returned n*n. But the results are still different if you look carefully: In [2]: def sq(n): ...: y=n*n ...: print y ...: ...: In [3]: map(sq, range(3)) 0 1 4 Out[3]: [None, None, None] The function sq() is called for each element of range(3) and prints the square. This is why 0, 1, 4 are printed. But the value returned from map() is the list [None, None, None] which is the accumulated return values from calling sq(). In [4]: filter(sq, range(3)) 0 1 4 Out[4]: [] Here, sq() is still called for each element of range(3). Since the printing is from sq(), 0, 1 and 4 are still printed. But the return value is an empty list [] because None is not true so sq(n) is not true for any elements of range(3). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: i want to know the difference between filter(function,sequence) and map(function,sequence). print filter.__doc__ filter(function or None, sequence) - list, tuple, or string Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list. print map.__doc__ map(function, sequence[, sequence, ...]) - list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence). filter returns a subsequence of a sequence based on passing each item in the sequence to a function which returns a *boolean context*. If the returns value's boolean context is true, the item is placed in the new subsequence. map returns a sequence of the same length based on the return value of passing each item in the sequence to a function. One literally filters a sequence. The other literally maps a sequence. filter can return a tuple, string, or list. map only returns a list. I tried for a simple script with an function which finds the square of the number,after including separately filter and map in the script i am getting the same results for instance def squ(x): return x*x filter(squ,range(1,3))-1,4(output) map(squ,range(1,3)-1,4(output) The boolean context of the return value of squ is true for all items of the sequence which you passed it in filter. Also, the filter you showed above does *not* return [1,4]. It returns [1,2], which is every item in range(1,3), because every item in that list passes the filter function's boolean context (is x*x true?). -- Sincerely, Chris Calloway http://www.seacoos.org office: 332 Chapman Hall phone: (919) 962-4323 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: ya i am sure about that i am using python editor which has python intrepreter attached to it i got the same output for both filter and map def squ(n): y = n*n print y filter(y,range(3))-0 1 4 map(y,range(3))-0 1 4 You are not printing the result of either the filter or map function here. You have the print statement embedded in squ. In fact you wouldn't print anything but a NameError exception here because you haven't passed filter or map a function, just an identifier which isn't in their scope: def squ(n): ...y = n*n ...print y ... filter(y, range(3)) Traceback (most recent call last): File stdin, line 1, in ? NameError: name 'y' is not defined Also not, the function squ as defined here always returns None, so it is useless as either a filtering or mapping function. Printing a value is not the same as returning a value. -- Sincerely, Chris Calloway http://www.seacoos.org office: 332 Chapman Hall phone: (919) 962-4323 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
Yes i did a mistake in expressing my problem below are the instances of the script and its corresponding output,for each instance its giving contrasting result i want explanation for that [1]:def squ(n): return n*n filter(squ,range(3))output is not seen on the interpreter map(squ,range(3))-output not seen on the interpreter print filter(squ,range(3))-output is [1,2] print map(squ,range(3))--output is [0,1,4] [2]:def squ(n): y = n*n print y filter(squ,range(3))--Below is the output 0 1 4 map(squ,range(3))--Below is the output 0 1 4 print filter(squ,range(3))---Below is the output 0 1 4 [] print map(squ,range(3))--Below is the output 0 1 4 [None,None,None] I want to know why in each case its giving different results and diff between filter and map On 1/23/07, Kent Johnson [EMAIL PROTECTED] wrote: vanam wrote: ya i am sure about that i am using python editor which has python intrepreter attached to it i got the same output for both filter and map def squ(n): y = n*n print y filter(y,range(3))-0 1 4 map(y,range(3))-0 1 4 This is quite different that what you posted the first time. This function squ() *prints* n*n but *returns* None (since it has no explicit return statement). The previous squ() actually returned n*n. But the results are still different if you look carefully: In [2]: def sq(n): ...: y=n*n ...: print y ...: ...: In [3]: map(sq, range(3)) 0 1 4 Out[3]: [None, None, None] The function sq() is called for each element of range(3) and prints the square. This is why 0, 1, 4 are printed. But the value returned from map() is the list [None, None, None] which is the accumulated return values from calling sq(). In [4]: filter(sq, range(3)) 0 1 4 Out[4]: [] Here, sq() is still called for each element of range(3). Since the printing is from sq(), 0, 1 and 4 are still printed. But the return value is an empty list [] because None is not true so sq(n) is not true for any elements of range(3). Kent -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor