dict slice in python (translating perl to python)

Wojtek Walczak gminick at bzt.bzt
Wed Sep 10 12:14:53 EDT 2008


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}
>>> map(myhash.get, ['one', 'two', 'two'])
[1, 2, 2]
>>>

or, to make it more similar to perl's qw() thing:

>>> map(myhash.get, 'one two two'.split())
[1, 2, 2]
>>>

...but I think that using explicit list is more pythonic.
Isn't it? ;)

-- 
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/



More information about the Python-list mailing list