列表推导式中的条件。为什么当我使用“else”时会有所不同?
代码 A:
numbers = [2, -1, 79, 33, -45]
negative_doubled = [num * 2 for num in numbers if num < 0]
print(negative_doubled) # output is: [-2, -90]
代码 B:
numbers = [2, -1, 79, 33, -45]
doubled = [num * 2 if num < 0 else num * 3 for num in numbers ]
print(doubled) # output is: [6, -2, 237, 99, -90]
如您所见,if语句的位置是不同的。在代码 A 中,该if语句位于 for 循环语句之后。
他们为什么不写这样的代码?我会觉得它更直观。
numbers = [2, -1, 79, 33, -45]
negative_doubled = [num * 2 if num < 0 for num in numbers] # SyntaxError
print(negative_doubled)
(如您所知,这个位置是一个语法错误。)
有没有可能出现问题的情况?
回答
您可以认为 Python 中的推导式始终遵循以下通用格式:
[expression for iter_var in sequence if some_condition]
[expression for iter_var in sequence if some_condition]
除非为 给出明确的布尔表达式some_condition,否则假定为真。
的expression可以是在Python任何有效的表达,包括三元表达,下面的格式
x = val if cond else other_val
这相当于
if cond:
x = val
else:
x = other_val
一种理解是一个简单的过滤器,[x for x in seq if cond]。另一种是三元表达式[x if cond else y for _ in seq]。
过滤理解的示例:
>>> [x for x in range(10) if x % 2 == 0]
[0, 2, 4, 6, 8]
这相当于
res = []
for x in range(10):
if x % 2 == 0:
res.append(x)
下面是一个三元理解的例子:
>>> [x**2 if x % 2 == 0 else x**3 for x in range(10)]
[0, 1, 4, 27, 16, 125, 36, 343, 64, 729]
这相当于
res = []
for x in range(10):
res.append(x**2 if x % 2 == 0 else x**3)
这是两者结合的示例:
>>> lst = [1, "a", "b", 3, "2", 6]
>>> [x**2 if x % 2 == 0 else x**3 for x in lst if isinstance(x, int)]
[1, 27, 36]
这相当于
res = []
for x in range(10):
if isinstance(x, int):
res.append(x**2 if x % 2 == 0 else x**3)
TL; 博士
您可以将推导理解为实际上具有一个统一的结构:
theexpression可以是任何东西,甚至是一个三元表达式,它本身包含ifandelse关键字,但实际上与some_condition所有推导式所基于的都无关。
我不知道,我喝醉了,而且我觉得我在解释方面做得不好,也不想再尝试了。
- But OP clearly *doesn't* understand how comprehensions work. One uses a ternary assignment expression for the value that gets collected, one uses a filtering statement. This answer attempts to explain the differences.
- @juanpa.arrivillaga yes that's a good answer you could have contributed. It's not the only answer, though. IMHO if he knew the difference between the two (that they are *different* `if`s) then he wouldn't have asked that question.