[IronPython] Unpacking

Jarek Śmiejczak groups at pythonalchemy.com
Wed Sep 8 16:41:39 CEST 2010


Dnia 2010-09-08, śro o godzinie 08:13 -0600, Vernon Cole pisze:
> If you bring up Python in interactive mode, and type "import this",
> some sage advice will appear:
> v v v v v
> C:\Users\vernon>ipy
> C:\Users\vernon>"c:\program files\IronPython 2.7\ipy.exe"
> IronPython 2.7 Alpha 1 (2.7.0.1) on .NET 4.0.30319.1
> >>> import this
> 
> Explicit is better than implicit.
> Simple is better than complex.
> Readability counts.
> ^ ^ ^ ^
> 
> In that spirit, may I suggest the following simple, readable code?
> <code>
> ml = ['name1','x1','y1','name2','x2','y2']
> i = 0
> d = {}
> while True:
>     try:
>         d[ml[i]] = (ml[i+1],ml[i+2])
>     except IndexError:
>         break
>     i += 3
> print repr(d)
> </code>


Hi, 
More "compact" version for perl lovers:
d = dict([ [ml[i],[ml[i+1], ml[i+2]]] for i in xrange(0, len(ml), 3)]) 

;)
--------
Greetings





More information about the Ironpython-users mailing list