小编Inc*_*ito的帖子

在单行for循环语句中跳过元素

有没有一种方法可以在使用时跳过for循环的迭代

[x if cond else pass for x in seq]
Run Code Online (Sandbox Code Playgroud)

尝试时出现语法错误。

Output >>> File "<ipython-input-122-a943adcf1b68>", line 1
            test = [pass if isinstance(x, float) else x for x in test_list]
                       ^
      SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

python list-comprehension python-3.x

-2
推荐指数
1
解决办法
50
查看次数

标签 统计

list-comprehension ×1

python ×1

python-3.x ×1