Is There '?' Control Flow in Python?

Possible Duplicate: Python Ternary Operator Is there control flow operator similar to '?' of C/C++ in python? If there is a chunk of code similar to this: return n <= 1 ? n : fibo(n-1) + fibo(n-2) Will got an error like this: File "fibonacci.py", line 2 return n <= 1 ? n : fibo(n-1) + fibo(n-2) ^ SyntaxError: invalid syntax 是的,条件表达式在Python 2.

在那儿 '?' Python中的控制流?

可能重复: Python三元运算符 控制流操作符是否与'?'类似 C / C ++在python中的使用? 如果有一段代码与此类似: return n <= 1 ? n : fibo(n-1) + fibo(n-2) 会有这样的错误: File "fibonacci.py", line 2 return n <= 1 ? n : fibo(n-1) + fibo(n-2) ^ SyntaxError: invalid syntax 是的,条件表达式在Python 2.5+中可用: return n if n <= 1 else fibo(n-1) + fibo(n-2)

python: iif or (x ? a : b)

Possible Duplicate: Python Ternary Operator If Python would support the (x ? a : b) syntax from C/C++, I would write: print paid ? ("paid: " + str(paid) + " €") : "not paid" I really don't want to have an if-check and two independent prints here (because that is only an example above, in my code, it looks much more complicated and would really be stupid to have almost the same code twic

python:iif或(x?a:b)

可能重复: Python三元运算符 如果Python支持来自C / C ++的(x?a:b)语法,我会写: print paid ? ("paid: " + str(paid) + " €") : "not paid" 我真的不想在这里有一个if-check和两个独立的打印(因为这只是上面的一个例子,在我的代码中,它看起来更加复杂,并且实际上有两个几乎相同的代码是愚蠢的)。 但是,Python不支持这个操作符或任何类似的操作符(afaik)。 什么是最简单/最干净/最常见的方式来做到这一点

python (bool) ? then : else syntax?

Possible Duplicate: Python Ternary Operator In some languages including Java, C/C++, C#, etc. you can assign a value based on the result of an inline boolean expression. For example, return (i < x) ? i : x This will return i if i < x, otherwise it will return x. I like this because it is much more compact in many cases than the longer syntax which follows. if (i < x) return i

蟒蛇(布尔)? 那么:else语法?

可能重复: Python三元运算符 在包括Java,C / C ++,C#等在内的一些语言中,您可以根据内联布尔表达式的结果分配值。 例如, return (i < x) ? i : x 这将返回我如果我<x,否则它会返回x。 我喜欢这个,因为它在很多情况下比下面更长的语法更紧凑。 if (i < x) return i else return x 是否有可能在Python中使用这种语法,如果是这样,如何? 你可以使用(x if cond else y) ,例如 >>> x =

What's the best way to replace the ternary operator in Python?

Possible Duplicate: Ternary conditional operator in Python If I have some code like: x = foo ? 1 : 2 How should I translate it to Python? Can I do this? if foo: x = 1 else: x = 2 Will x still be in scope outside the if / then blocks? Or do I have to do something like this? x = None if foo: x = 1 else: x = 2 在Python 2.5+中使用三元运算符(正式条件表达式)。 x = 1 if foo else 2

在Python中替换三元运算符的最好方法是什么?

可能重复: Python中的三元条件运算符 如果我有这样的代码: x = foo ? 1 : 2 我应该如何将它翻译成Python? 我可以这样做吗? if foo: x = 1 else: x = 2 x还会在if / then块之外的范围内吗? 还是我必须做这样的事情? x = None if foo: x = 1 else: x = 2 在Python 2.5+中使用三元运算符(正式条件表达式)。 x = 1 if foo else 2 所提到的三元运算符只能从Python 2.5中获得。 来自WeekeePeedeea: 虽

Python ternary operator

Possible Duplicate: Ternary conditional operator in Python var foo = (test) ? "True" : "False"; What would this look like in Python? Using Python 2.7 if that makes a difference. PEP 308 adds a ternary operator: foo = "True" if test else "False" It's been implemented since Python 2.5 这个看起来更像原始三元: foo=a and b or c

Python三元运算符

可能重复: Python中的三元条件运算符 var foo = (test) ? "True" : "False"; 这在Python中看起来像什么? 使用Python 2.7,如果这有所作为。 PEP 308增加了一个三元运算符: foo = "True" if test else "False" 从Python 2.5开始就已经实现了 这个看起来更像原始三元: foo=a and b or c

equivalent of `a?b:c`

Possible Duplicate: Python Ternary Operator I want to print out a string in python. I don't want to do: if isfemale_bit: print 'F' else: print 'M' The best I have right now is print ['M', 'F'][int(isfemale_bit)] ? Is there a better alternative? I needs my syntactic sugar!! In Python 2.5, you can use ternary conditionals like this: a if b else c There is

相当于`a?b:c`

可能重复: Python三元运算符 我想在python中输出一个字符串。 我不想这样做: if isfemale_bit: print 'F' else: print 'M' 现在最好的是print ['M', 'F'][int(isfemale_bit)] ? 有更好的选择吗? 我需要我的语法糖! 在Python 2.5中,你可以使用这样的三元条件: a if b else c 这里有更多的讨论:Python是否有三元条件运算符? 啊三元运营商: >>> print 'foo' if True els

python ? (conditional/ternary) operator for assignments

This question already has an answer here: Does Python have a ternary conditional operator? 21 answers Python has such an operator: variable = something if condition else something_else Alternatively, although not recommended (see @karadoc's comment): variable = (condition and something) or something_else In older Python code, you may see the trick: condition and something or somethi

python? (条件/三元)操作员

这个问题在这里已经有了答案: Python是否有三元条件运算符? 21个答案 Python有这样一个运算符: variable = something if condition else something_else 或者,尽管不推荐(请参阅@ karadoc的评论): variable = (condition and something) or something_else 在较老的Python代码中,您可能会看到这样的技巧: condition and something or something_else 然而,这已被大大优越的... if ... else ...构造所取代:

Python if

Possible Duplicate: Ternary conditional operator in Python I want to do the following in python: while( i < someW && j < someX){ int x = A[i] > B[j]? A[i++]:B[j++]; .... } Clearly, when either i or j hits a limit, the code will break out of the loop. I need the values of i and j outside of the loop. Must I really do x=0 ... if A[i] > B[j]: x = A[i] i+=1 els

Python如果

可能重复: Python中的三元条件运算符 我想在python中执行以下操作: while( i < someW && j < someX){ int x = A[i] > B[j]? A[i++]:B[j++]; .... } 显然,当i或j达到极限时,代码就会跳出循环。 我需要循环之外的i和j的值。 我真的该做吗? x=0 ... if A[i] > B[j]: x = A[i] i+=1 else: x = B[j] j+=1 还是有人知道更短的路? 除了上面的内容,我可以让Python支持类似的东西

How to condense if/else into one line in Python?

Possible Duplicate: Python Ternary Operator Putting a simple if-then statement on one line Is there a way to compress an if/else statement to one line in Python? I oftentimes see all sorts of shortcuts and suspect it can apply here too. An example of Python's way of doing "ternary" expressions: i = 5 if a > 7 else 0 translates into if a > 7: i = 5 else: i = 0

如何将if / else压缩成Python中的一行?

可能重复: Python三元运算符 将简单的if-then语句放在一行上 有没有办法在Python中将if / else语句压缩到一行? 我经常看到各种各样的捷径,并且怀疑它也可以在这里适用。 Python做“三元”表达式的一个例子: i = 5 if a > 7 else 0 翻译成 if a > 7: i = 5 else: i = 0 当使用列表推导,或者有时在返回语句中,这实际上很方便,否则我不确定它在创建可读代码方面有多大帮助。 可读性问题在这个最近

Putting a simple if

Possible Duplicate: Python Ternary Operator I'm just getting into Python and I really like the terseness of the syntax. However, is there an easier way of writing an if - then - else statement so it fits on one line? For example: if count == N: count = 0 else: count = N + 1 Is there a simpler way of writing this? I mean, in Objective-C I would write this as: count = count =

简单的如果

可能重复: Python三元运算符 我刚刚进入Python,我非常喜欢语法的简洁。 但是,是否有更简单的方法来编写if - then - else语句,以便它适合于一行? 例如: if count == N: count = 0 else: count = N + 1 有没有更简单的方式来写这个? 我的意思是,在Objective-C中,我会将其写为: count = count == N ? 0 : count + 1; Python有没有类似的东西? 更新 我知道在这种情况下,我可以使用count == (cou