Re: dict slice in python (translating perl to python}

2008-09-12 Thread Hendrik van Rooyen
MRAB googlarnett.plus.com wrote: On Sep 11, 6:11 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: [snip] (the next step towards true Pythonicness would be to store your data in 8--- Surely the word is Pythonicity? :-) When faced with the choice between Pythonicness and

Re: dict slice in python (translating perl to python)

2008-09-12 Thread Nick Craig-Wood
Steven D'Aprano [EMAIL PROTECTED] wrote: On Thu, 11 Sep 2008 03:36:35 -0500, Nick Craig-Wood wrote: As an ex-perl programmer and having used python for some years now, I'd type the explicit v1,v2,v3 = mydict['one'], mydict['two'], mydict['two'] # 54 chars Or maybe even

Re: dict slice in python (translating perl to python)

2008-09-11 Thread Duncan Booth
hofer [EMAIL PROTECTED] wrote: Let's take following perl code snippet: %myhash=( one = 1, two = 2, three = 3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # -- line of interest print $v1\n$v2\n$v2\n; How do I translate the second line in a similiar compact way to python? One

Re: dict slice in python (translating perl to python)

2008-09-11 Thread Nick Craig-Wood
hofer [EMAIL PROTECTED] wrote: Let's take following perl code snippet: %myhash=( one = 1, two = 2, three = 3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # -- line of interest print $v1\n$v2\n$v2\n; How do I translate the second line in a similiar compact way to python?

Re: dict slice in python (translating perl to python)

2008-09-11 Thread Steven D'Aprano
On Thu, 11 Sep 2008 03:36:35 -0500, Nick Craig-Wood wrote: As an ex-perl programmer and having used python for some years now, I'd type the explicit v1,v2,v3 = mydict['one'], mydict['two'], mydict['two'] # 54 chars Or maybe even v1 = mydict['one'] # 54 chars v2 = mydict['two']

Re: dict slice in python (translating perl to python)

2008-09-11 Thread hofer
Thanks a lot for all your answers. There's quite some things I learnt :-) [v1,v2,v3] = ... can be typed as v1,v2,v3 = . . . I also wasn't used to map(myhash.get, ['one', 'two', 'two']) itemgetter('one', 'one', 'two')(x) I also didn't know print %(one)s\n%(two)s\n%(two)s % mydict The reason

Re: dict slice in python (translating perl to python)

2008-09-11 Thread hofer
On Sep 11, 10:36 am, Nick Craig-Wood [EMAIL PROTECTED] wrote: I'd type the explicit v1,v2,v3 = mydict['one'], mydict['two'], mydict['two'] # 54 chars Either is only a couple more characters to type.  It is completely explicit and comprehensible to everyone, in comparison to   v1,v2,v3 =

Re: dict slice in python (translating perl to python)

2008-09-11 Thread bearophileHUGS
hofer: The real example would be more like: name,age,country = itemgetter('name age country'.split())(x) # or any of my above versions That solution is very clever, and the inventor smart, but it's too much out of standard and complex to be used in normal real code. Learning tricks is useful,

Re: dict slice in python (translating perl to python)

2008-09-11 Thread Fredrik Lundh
hofer wrote: The real example would be more like: name,age,country = itemgetter('name age country'.split())(x) ouch. if you do this a lot (=more than once), just wrap your dictionaries in a simple attribute proxy, and use plain attribute access. that is, given class

Re: dict slice in python (translating perl to python)

2008-09-11 Thread Aaron Castironpi Brady
On Sep 11, 10:52 am, hofer [EMAIL PROTECTED] wrote: On Sep 11, 10:36 am, Nick Craig-Wood [EMAIL PROTECTED] wrote: I'd type the explicit  v1,v2,v3 = mydict['one'], mydict['two'], mydict['two'] # 54 chars Either is only a couple more characters to  type.  It is completely explicit and

Re: dict slice in python (translating perl to python)

2008-09-11 Thread MRAB
On Sep 11, 6:11 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: [snip] (the next step towards true Pythonicness would be to store your data in class instances instead of dictionaries in the first place, but one step at a time...) Surely the word is Pythonicity? :-) --

dict slice in python (translating perl to python)

2008-09-10 Thread hofer
Hi, Let's take following perl code snippet: %myhash=( one = 1, two = 2, three = 3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # -- line of interest print $v1\n$v2\n$v2\n; How do I translate the second line in a similiar compact way to python? Below is what I tried. I'm just

Re: dict slice in python (translating perl to python)

2008-09-10 Thread Wojtek Walczak
On Wed, 10 Sep 2008 08:28:43 -0700 (PDT), hofer wrote: Hi, Let's take following perl code snippet: %myhash=( one = 1, two = 2, three = 3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # -- line of interest print $v1\n$v2\n$v2\n; What about: myhash={'one':1, 'two':2, 'three':3}

Re: dict slice in python (translating perl to python)

2008-09-10 Thread Jon Clements
On 10 Sep, 16:28, hofer [EMAIL PROTECTED] wrote: Hi, Let's take following perl code snippet: %myhash=( one  = 1    , two   = 2    , three = 3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # -- line of interest print $v1\n$v2\n$v2\n; How do I translate the second line in a similiar compact

Re: dict slice in python (translating perl to python)

2008-09-10 Thread B
for a long list, you could try: result = [mydict[k] for k in mydict] or [mydict[k] for k in mydict.keys()] or [mydict[k] for k in mydict.iterkeys()] this won't give you the same order as your code though, if you want them sorted you can use the sorted function: [mydict[k]

Re: dict slice in python (translating perl to python)

2008-09-10 Thread Fredrik Lundh
B wrote: for a long list, you could try: result = [mydict[k] for k in mydict] or [mydict[k] for k in mydict.keys()] or [mydict[k] for k in mydict.iterkeys()] and the point of doing that instead of calling mydict.values() is what? /F --

Re: dict slice in python (translating perl to python)

2008-09-10 Thread B
Fredrik Lundh wrote: B wrote: for a long list, you could try: result = [mydict[k] for k in mydict] or [mydict[k] for k in mydict.keys()] or [mydict[k] for k in mydict.iterkeys()] and the point of doing that instead of calling mydict.values() is what? /F It's more fun? Or if

Re: dict slice in python (translating perl to python)

2008-09-10 Thread Terry Reedy
hofer wrote: Let's take following perl code snippet: %myhash=( one = 1, two = 2, three = 3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # -- line of interest print $v1\n$v2\n$v2\n; How do I translate the second line in a similiar compact way to python? Below is what I tried. I'm