怎么做“new_variable =(变量)?真:假;“ (php)在python上? [英] How to do "new_variable = (variable) ? True : False;" (php) on python?
问题描述
大家好,
我该怎么办
new_variable =(变量)?是的:错误;
用Python一行?
我想做这样的事情:
dic = {''item1'':(变量)?真实部分:假部分}
有什么建议吗?
Daniel
Hello to all,
How can I do
new_variable = (variable) ? True : False;
in Python in one line?
I want to do something like this:
dic = {''item1'': (variable) ? True-part : False-part}
Any suggestions?
Daniel
推荐答案
Daniel Crespo写道:
Daniel Crespo wrote:
我该怎么办
new_variable =(变量)?没错:错误;
在Python中一行?
How can I do
new_variable = (variable) ? True : False;
in Python in one line?
new_variable = variable
: - )
Peter
new_variable = variable
:-)
Peter
Daniel Crespo写道:
Daniel Crespo wrote:
我该怎么办?
new_variable =(变量)?没错:错误;
在Python中一行?
How can I do
new_variable = (variable) ? True : False;
in Python in one line?
new_variable = variable
: - )
Peter
new_variable = variable
:-)
Peter
Daniel Crespo写道:
Daniel Crespo wrote:
大家好,
我该怎么办
new_variable =(变量)?是的:错误;用Python在一行中?
我想做这样的事情:
dic = {''item1'' :(变量)?真实部分:假部分}
任何建议?
Daniel
Hello to all,
How can I do
new_variable = (variable) ? True : False;
in Python in one line?
I want to do something like this:
dic = {''item1'': (variable) ? True-part : False-part}
Any suggestions?
Daniel
有一个技巧使用短路布尔逻辑运算符
模拟Python中的三元运算符。基本上,你做了
TEST和TRUE_PART或FALSE_PART
但是,如果TRUE_PART评估为False值,则会失败;你用FALSE_PART的值来结束
。
这是一个技巧/黑客,但不应该真的用得太多 - 改为使用
if语句,或者当if表达式为
时,请等待2.5。
There''s a trick using the short-circuiting boolean logic operators to
emulate the ternary operator in Python. Basically, you do
TEST and TRUE_PART or FALSE_PART
However, this fails if TRUE_PART evaluates to a False value; you end up
with FALSE_PART''s value instead.
This is a trick/hack, though, and shouldn''t really be used much - use
an if statement instead, or wait til 2.5 when an if expression is
coming in.
这篇关于怎么做“new_variable =(变量)?真:假;“ (php)在python上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!