A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.
在本例中,表达式是2 * x,然后是for语句,for x in some_list,然后是if语句,if x > 2。
这种理解是可以理解的,就像这样
result = []
for x in some_list:
if x > 2:
result.append(x)
把条件移到最后
引用List Comprehension documentation
在本例中,表达式是
2 * x
,然后是for
语句,for x in some_list
,然后是if
语句,if x > 2
。这种理解是可以理解的,就像这样
相关问题 更多 >
编程相关推荐