C"< test>中的复合语句?< true-val>:< false-val>" [英] compound statement from C "<test>?<true-val>:<false-val>"

查看:62
本文介绍了C"< test>中的复合语句?< true-val>:< false-val>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我一直无法弄清楚如何从C进行复合声明
- "< test>?< true -val>:< false-val>"


但类似的东西必须存在......?!


我想要做等效的C行的python:

printf(我看到%d car%s \ n,n,n!= 1?" s":"" )


请帮助


/ Holger

解决方案

http: //effbot.org/pyfaq/is-there-an-...y-operator.htm

-

EduardoOPadoan( eopadoan-> altavix :: com)

书签: http:/ /del.icio.us/edcrypt

博客: http: //edcrypt.blogspot.com

Jabber:edbpt at jabber dot org

ICQ:161480283

GTalk:eduardo dot padoan at gmail dot com

MSN:etaadoan at altavix dot com


Holger kirjoitti:


>

如果是C行的python,我想做同等的事情:

printf(我看到%d car%s \ n,n,n!= 1? " S" :"")


请帮助


/ Holger



在这种特殊情况下,你不需要三元运算符:


print"我看到%d car%s \ n" %(n,("," s")[n!= 1])

干杯,

Jussi

< br>

Jussi Salmela:


在这种特殊情况下,你不需要三元运算符:

print"我看到了%d car%s \ n %(n,("," s")[n!= 1])$ ​​b $ b



最后一个换行符可能是不必要的。这似乎有点多了

可读:

print" I saw",n," car" +(""," s")[n!= 1]


使用Python 2.5,这看起来更好:

print" I seen" ;,n,汽车; +("" if n == 1 else" s")


或者我更喜欢的版本:

print" I seen" ,n,(汽车,如果n == 1其他汽车)


那些()不是必需的,但它们有助于提高可读性,并且

也避免了运算符优先级问题。如果有一个相当低的

优先。


再见,

。熊市场


Hi

I have not been able to figure out how to do compound statement from C
- "<test>?<true-val>:<false-val>"

But something similar must exist...?!

I would like to do the equivalent if python of the C line:
printf("I saw %d car%s\n", n, n != 1 ? "s" : "")

Please help

/Holger

解决方案

http://effbot.org/pyfaq/is-there-an-...y-operator.htm

--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com


Holger kirjoitti:

>
I would like to do the equivalent if python of the C line:
printf("I saw %d car%s\n", n, n != 1 ? "s" : "")

Please help

/Holger

In this particular case you don''t need the ternary operator:

print "I saw %d car%s\n" % (n, ("", "s")[n != 1])
Cheers,
Jussi


Jussi Salmela:

In this particular case you don''t need the ternary operator:
print "I saw %d car%s\n" % (n, ("", "s")[n != 1])

The last newline is probably unnecessary. This seems be a bit more
readable:
print "I saw", n, "car" + ("", "s")[n != 1]

With Python 2.5 this looks better:
print "I saw", n, "car" + ("" if n == 1 else "s")

Or the vesion I like better:
print "I saw", n, ("car" if n == 1 else "cars")

Those () aren''t necessary, but they help improve readability, and
avoid problems with operator precedence too. That if has a quite low
precedence.

Bye,
bearophile


这篇关于C&quot;&lt; test&gt;中的复合语句?&lt; true-val&gt;:&lt; false-val&gt;&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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