[IronPython] Unpacking

Steve Dower s.j.dower at gmail.com
Wed Sep 8 23:29:05 CEST 2010


Since we're all contributing here:

d = { key: (v1, v2) for key, v1, v2 in zip(seq[::3], seq[1::3], seq[2::3]) }

Or

d = dict((key, (v1, v2)) for key, v1, v2 in zip(seq[::3], seq[1::3], seq[2::3])

using Python 2.6. The first uses a dictionary comprehension, which is
new in 2.7/3 (but not IronPython 2.7 it would seem...)

On Thu, Sep 9, 2010 at 04:08, Iivari Mokelainen <iivari at mokelainen.com> wrote:
> I thought you guys would suggest something like this
>
> def parseOffsets(str):
>     seq = str.split()
>     dict = {}
>     while seq != []:
>         name, x, y = seq[:3]
>         dict[name] = (int(x),int(y))
>         del seq[:3]
>     return dict
>
> which seems to me as something in more of python-spirit? :D
>
> well, anyways, thanks - now i know i dont have to bend backwards with
> restricting myself from using simple old paradigms (like for loop) in my
> python code (:
>
> Cheers!
>
> On 08/09/2010 17:13, Vernon Cole wrote:
>
> 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>
>
> On Wed, Sep 8, 2010 at 5:29 AM, Iivari Mokelainen <iivari at mokelainen.com>
> wrote:
>>
>>  I need to unpack a list of
>>    [name, x, y, name, x, y]
>> to a dictionary
>>    {name : (x,y), name:(x,y)}
>> how would one do that?
>>
>> Also, is it ok to ask such newbie questions here, or is there a better
>> place for that?
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>



More information about the Ironpython-users mailing list