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? > > Below is what I tried. I'm just interested in something more compact. > > mydict={ 'one' : 1 , 'two' : 2 , 'three' : 3 } > # first idea, but still a little too much to type > [v1,v2,v3] = [ mydict[k] for k in ['one','two','two']] > > # for long lists lazier typing,but more computational intensive > # as split will probably be performed at runtime and not compilation > time > [v1,v2,v3] = [ mydict[k] for k in 'one two two'.split()]
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'] v3 = mydict['two'] Either is only a couple more characters to type. It is completely explicit and comprehensible to everyone, in comparison to v1,v2,v3 = [ mydict[k] for k in ['one','two','two']] # 52 chars v1,v2,v3 = [ mydict[k] for k in 'one two two'.split()] # 54 chars Unlike perl, it will also blow up if mydict doesn't contain 'one' which may or may not be what you want. -- Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list