dict slice in python (translating perl to python)

Duncan Booth duncan.booth at invalid.invalid
Thu Sep 11 10:08:45 CEST 2008


hofer <blabla at dungeon.de> 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 point I haven't seen in the other responses is that, at least for the 
example given, you don't need the second line at all:

 mydict={ 'one': 1, 'two': 2, 'three': 3 }
 print "%(one)s\n%(two)s\n%(two)s" % mydict

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list