三级运作 [英] Tertiary Operation
问题描述
x =无
结果=(x为无和&或str(x))
打印结果,类型(结果)
---------------
输出
------ ---------
无< type''str''>
y = 5
result =(y是5和它'是5或它不是5)
打印结果
- -----------
输出
-------------
it '是五个
"结果"是一个空字符串,而不是无的str值。
x = None
result = (x is None and "" or str(x))
print result, type(result)
---------------
OUTPUT
---------------
None <type ''str''>
y = 5
result = (y is 5 and "it''s five" or "it''s not five")
print result
-------------
OUTPUT
-------------
it''s five
....what''s wrong with the first operation I did with x? I was expecting
"result" to be an empty string, not the str value of None.
推荐答案
" abcd" < co ******* @ gmail.comwrote in message
news:11 ********************** @ b28g2000cwb.googlegr oups.com ...
"abcd" <co*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
> x =无
result =(x为None和"或str(x))
...我用x做的第一次操作有什么问题?我期待
"结果"是一个空字符串,而不是无的str值。
>x = None
result = (x is None and "" or str(x))
...what''s wrong with the first operation I did with x? I was expecting
"result" to be an empty string, not the str value of None.
你的邪恶三级黑客已经失败了,因为空字符串
在布尔上下文中计为false。请学会喜欢新的条件表达式语法:
http://docs.python.org/whatsnew/pep-308.html
x =无
x = None
result =(x为None和"或str(x))
打印结果,类型(结果) )
---------------
输出
----- ----------
无<类型''str''>
y = 5
result =(y为5,它'为5或它不是5)
打印结果
-------------
输出
-------------
它是五个
"结果"是一个空字符串,而不是无的str值。
result = (x is None and "" or str(x))
print result, type(result)
---------------
OUTPUT
---------------
None <type ''str''>
y = 5
result = (y is 5 and "it''s five" or "it''s not five")
print result
-------------
OUTPUT
-------------
it''s five
...what''s wrong with the first operation I did with x? I was expecting
"result" to be an empty string, not the str value of None.
空字符串的计算结果为False,因此它继续到
其他分支。以下任何一个都应该足够了:
#返回一个非空字符串
x是None和None或者str(x)
#颠倒逻辑并返回
#在和中的东西部分
x不是None和str(x)或"
在
$中有更多巴洛克式的编写三元运算符的方式b $ b python(虽然我理解2.5或者python 3k应该有
a这样做的真实方式)。我的理解是,一个常见的
解决方案类似于
{True:"",False:str(x)} [x is None]
-tkc
An empty string evaluates to False, so it then continues to the
other branch. Either of the following should suffice:
# return a non-empty string
x is None and "None" or str(x)
# invert the logic and return
# something in the "and" portion
x is not None and str(x) or ""
There are more baroque ways of writing the terniary operator in
python (although I understand 2.5 or maybe python 3k should have
a true way of doing this). My understanding is that one common
solution is something like
{True: "", False: str(x)}[x is None]
-tkc
On Tue,2006-10-17 at 09:30,abcd写道:
On Tue, 2006-10-17 at 09:30, abcd wrote:
x =无
result =(x为None和"或str(x))
打印结果,类型(结果)
---------------
OUTPUT >
---------------
无< type''str''>
x = None
result = (x is None and "" or str(x))
print result, type(result)
---------------
OUTPUT
---------------
None <type ''str''>
条件和结果1或结果2如果result1是具有True布尔值的
表达式,则该技巧才有效。空字符串有一个错误的
布尔值。
您可以通过将其粘贴到
$ b $中来强制result1具有真正的布尔值ba list:
result =(x是None和["]或[str(x)])[0]
但这很难看。使用Python 2.5,其中有一个真正的条件
表达式或找到另一种方法来解决你的问题。
-Carsten
The "condition and result1 or result2" trick only works if result1 is an
expression with a True boolean value. The empty string has a false
boolean value.
You could force result1 to have a true boolean value by sticking it into
a list thusly:
result = (x is None and [""] or [str(x)])[0]
But that''s ugly. Use Python 2.5 where there is a true conditional
expression or find another way to solve your problem.
-Carsten
这篇关于三级运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!