Re: Lambda (Was Re: [scripting] The Models property in Python...)
| Date : Fri, 11 Aug 2006 09:10:00 +1000 |
| To : XSI(at)Softimage.COM |
| From : "Aloys Baillet" <aloys.baillet(at)gmail.com> |
| Subject : Re: Lambda (Was Re: [scripting] The Models property in Python...) |
My favourite use of the lambda is in the key argument of the sort function.
Let's say you want the list of all x3dobject in your scene, sorted by FullName.
import win32com.client
import pprint
col = win32com.client.Dispatch('XSI.Collection')
col.Items = '*'
objects = list(col)
objects.sort(key = lambda x: x.FullName)
# The list contains the X3DObjects
Application.LogMessage(pprint.pformat(objects))
# And they are sorted by FullName!
Application.LogMessage(pprint.pformat(map(lambda x:x.FullName, objects)))
Cheers,
Aloys
On 8/11/06, Martin BÃlanger <martinb@softimage.com> wrote:
Don't worry, he himself confirm, in his video.google.com presentation, that Lambda will stay for 3k.
From: owner-xsi@Softimage.COM [mailto:owner-xsi@Softimage.COM] On Behalf Of Chris Trimble
Sent: Thursday, August 10, 2006 1:27 PM
To: XSI@Softimage.COM
Subject: Re: Lambda (Was Re: [scripting] The Models property in Python...)On 8/10/06, Bradley Gabe <withanar@stanwinston.com> wrote:As a relative neophyte to Python, could somebody show me an example of
when something like lambda becomes necessary? Its structure and syntax
looks like an anomaly compared to the rest of Python.Yes, IIRC Guido has claimed it was slipped into the language by a lisp aficionado when he wasn't paying attention. :) I think it's one of the things he wants to get rid of in Python 3K.The syntax is just the word "lambda", then the variable name(s) you'd like to supply to the function, colon, then the function body. The lambda command returns a function object.lambda x: x+1Creates a function that adds 1 to the input.So try this in a python shellmyadder = lambda x: x+1myadder(1)result: 2Lambda is really handy when put in combination with map and filter. These allow you to write a small function to run over a list and avoid doing "for i in ..." etc..Map = run the function and put the result in a listFilter = the function returns a boolean, if true, that item will remain in the listReturn a list with each number incremented by one:a = [1, 2, 3]map(lambda x: x+1, a)result: [2, 3, 4]Return a list with results less than 3:a=[1, 2, 3]filter(lambda x: x<3, a)result [1,2]I actually use lambda all of the time and hope it doesn't get removed in Python 3K.- Chris
--
Aloys Baillet - XSI Technical Director
Character Dpt - Animal Logic
--
- References:
- RE: Lambda (Was Re: [scripting] The Models property in Python...)
- From: Martin Bélanger <martinb(at)Softimage.COM>
- RE: Lambda (Was Re: [scripting] The Models property in Python...)
| DATE: | << | >> | THREAD: | << | >> | INDEX: | Main | Thread |
|---|
- Previous by Date: RE: Quick Softimage Survey - Linux Setup
- Next by Date: RE: Quick Softimage Survey - Linux Setup
- Previous by Thread: RE: Quick Softimage Survey - Linux Setup
- Next by Thread: RE: Quick Softimage Survey - Linux Setup
- Index(es):
| Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available. |