列表理解中的条件。为什么我使用 'else' 时不一样?
Conditions in list comprehensions. Why is it different when I use '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)
(如您所知,此位置是一个语法错误。)
有没有可能出现问题的情况?
numbers = [2, -1, 79, 33, -45]
negative_doubled = [num * 2 if num < 0 else num for num in numbers] # SyntaxError
print(negative_doubled)
如果 num >= 0,你需要告诉你的机器发生了什么。
您可以认为 Python 中的理解始终遵循以下一般格式:
[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;DR
你可以认为推导实际上有一个单一的统一结构:
[expression for iter_var in sequence if some_condition]
expression
可以是任何东西,甚至是本身包含 if
和 else
关键字的三元表达式,但实际上与 some_condition
无关所有理解都是谓词。
我不知道,我有点醉了,我觉得我解释得不好,也不想再尝试了。
代码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)
(如您所知,此位置是一个语法错误。)
有没有可能出现问题的情况?
numbers = [2, -1, 79, 33, -45]
negative_doubled = [num * 2 if num < 0 else num for num in numbers] # SyntaxError
print(negative_doubled)
如果 num >= 0,你需要告诉你的机器发生了什么。
您可以认为 Python 中的理解始终遵循以下一般格式:
[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;DR
你可以认为推导实际上有一个单一的统一结构:
[expression for iter_var in sequence if some_condition]
expression
可以是任何东西,甚至是本身包含 if
和 else
关键字的三元表达式,但实际上与 some_condition
无关所有理解都是谓词。
我不知道,我有点醉了,我觉得我解释得不好,也不想再尝试了。