python中的dict切片(将perl转换为python) [英] dict slice in python (translating perl to python)

查看:156
本文介绍了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 "


这篇关于python中的dict切片(将perl转换为python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆