Kevin Carruthers on 20 Oct 2004 19:03:02 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] python, lambda, map


Jeff,

I think the problem is that the list returned from foo is a RHS value
(term?).  Many other languages generate compile time, or run time
errors when you attempt to modify a value like this.

Python allows you to operate on RHS values and to good benefit.  It
gives you the ability to write "/n".join([1, 2, 3,4]), etc.

In your case the list is probably being created as you expected, it is
just transient and you never see it - remember list.extent() modifies
a list in place, and doesn't return the list.

Because extend doesn't return a list, you need something else.  Try using '+':

L = map(lambda x: foo(x) + foo(x+2), L)

Hope this helps a little.  
Kevin

On Sun, 17 Oct 2004 19:52:27 -0400, Jeff Abrahamson <jeff@purple.com> wrote:
> In the below, I want L=[3,4,5,6] at the end.  Clearly that's not
> what's happening.
> 
> The problem is that I want to use a map with a lambda form, so I can't
> make assignments.
> 
>     >>> def foo(x):
>     ...     return [x,x+1]
>     ...
>     >>> foo(3)
>     [3, 4]
>     >>> foo(3).extend(foo(5))
>     >>> L=foo(3).extend(foo(5))
>     >>> L
>     >>>
> 
> I want to write something like this:
> 
>     L = map(lambda x: foo(x).extend(foo(x+2)), L)
> 
> Any thoughts?
> 
> I might have to break down and name this function...
> 
> --
>  Jeff
> 
>  Jeff Abrahamson  <http://www.purple.com/jeff/>    +1 215/837-2287
>  GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B
> 
>  A cool book of games, highly worth checking out:
>  http://www.amazon.com/exec/obidos/ASIN/1931686963/purple-20
> 
> 
>
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug