python中的dict切片(将perl转换为python)
让我们来看下面的perl代码片段:
%myhash =(one = 1,two = 2 ,3 = 3);
($ v1,$ v2,$ v3)= @myhash {qw(一二二)}; #< - 感兴趣的线路
打印" $ v1 \ n $ v2 \ n $ v2 \ n" ;;
怎么做我用类似的紧凑方式将第二行翻译为
python?
以下是我的尝试。我只是对更紧凑的东西感兴趣。
mydict = {''one'':1,''two'':2,''three'':3}
#第一个想法,但仍然有点太多,无法输入
[v1,v2,v3] = [mydict [k]为[...'一'中的k ,''两个'',''两个'']]
#用于长列表更懒的打字,但计算量更大
#as split可能在运行时执行而不是编译
时间
[v1,v2,v3] = [mydict [k]表示一二二中的k。分裂( )]
print"%s \ n%s \ n%s" %(v1,v2,v3)
感谢任何想法
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 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()]
print "%s\n%s\n%s" %(v1,v2,v3)
thanks for any ideas
v1 ,
v2,
v3)= @myhash {qw(one two two)}; #< - 感兴趣的线路
print"
v3) = @myhash{qw(one two two)}; # <-- line of interest
print "