?:在Python中 [英] ?: in Python
问题描述
如何在Python中优雅地做:
如果条件:
a = 1
else:
a = 2
喜欢C:
a =条件?1:2
How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
推荐答案
Il 2005-12-14,Andy Leszczynski< ya *** @ nospam.leszczynscy> ha scritto:
Il 2005-12-14, Andy Leszczynski <ya***@nospam.leszczynscy> ha scritto:
如何在Python中优雅地做:
如果条件:
a = 1
否则:
a = 2
喜欢在C:
a =条件?1:2
How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
这个新闻组有很多线程并在python-dev邮件
列表中有关三元运算符的信息。还有一个PEP AFAIK。
我喜欢这个:
在[1]中:switch = True
在[2]中:a =(1,2)[开关]
在[3]中:打印一个
2
-
劳伦斯 - http: //www.oluyede.org/blog
任何人都可以随心所欲地使用他想要的任何东西,但最后的光线是大部分的隧道
他的问题是Python"
There are tons of threads on this newsgroup and in the python-dev mailing
list about a ternary operator. There''s also a PEP AFAIK.
I like this:
In [1]:switch = True
In [2]:a = (1, 2)[switch]
In [3]:print a
2
--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Lawrence Oluyede写道:
Lawrence Oluyede wrote:
Il 2005-12-14,Andy Leszczynski< ya *** @nospam。 leszczynscy> ha scritto:
Il 2005-12-14, Andy Leszczynski <ya***@nospam.leszczynscy> ha scritto:
如何在Python中优雅地做:
如果条件:
a = 1
否则:
a = 2
喜欢C:
a =条件?1:2
How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
这个新闻组有大量的主题关于三元运算符的python-dev邮件列表。还有一个PEP AFAIK。
我喜欢这个:
在[1]中:switch = True
在[2]中: a =(1,2)[开关]
在[3]中:打印
2
There are tons of threads on this newsgroup and in the python-dev mailing
list about a ternary operator. There''s also a PEP AFAIK.
I like this:
In [1]:switch = True
In [2]:a = (1, 2)[switch]
In [3]:print a
2
也喜欢它,thx。开关不一定是bool,所以它比...更强大...:
A.
Like it too, thx. Switch does not have to be bool, so it is more
powerfull than ?:.
A.
>
2005年12月14日星期三21:16:23 +0100,Lawrence Oluyede< ra *** @ dot.com>写道:
On Wed, 14 Dec 2005 21:16:23 +0100, Lawrence Oluyede <ra***@dot.com> wrote:
Il 2005-12-14,Andy Leszczynski< ya *** @ nospam.leszczynscy> ha scritto:
Il 2005-12-14, Andy Leszczynski <ya***@nospam.leszczynscy> ha scritto:
如何在Python中优雅地做:
如果条件:
a = 1
否则:
a = 2
喜欢在C:
a = condition?1:2
How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
这个新闻组和python中有很多线程-dev邮件
关于三元运营商的列表。还有一个PEP AFAIK。
我喜欢这个:
在[1]中:switch = True
在[2]中: a =(1,2)[开关]
在[3]中:打印一个
2
There are tons of threads on this newsgroup and in the python-dev mailing
list about a ternary operator. There''s also a PEP AFAIK.
I like this:
In [1]:switch = True
In [2]:a = (1, 2)[switch]
In [3]:print a
2
你不喜欢它例如
a =(2 ** 20 ** 20,2)[开关]
或
a =(m / n,sys .maxint)[n == 0]
重点是if / else只在一个分支中计算表达式,就像C三元一样。
你是对的,有一个PEP和一个三元表达
虽然来到python。
问候,
Bengt Richter
You won''t like it in a case like
a = (2**20**20, 2)[switch]
or
a = (m/n, sys.maxint)[n==0]
the point is that if/else only evaluates
the expression in one branch, as with C ternary.
You''re right, there is a PEP and a ternary expression
coming to python though.
Regards,
Bengt Richter
这篇关于?:在Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!