小编mer*_*rpi的帖子

如何在Python代码中找到列表理解

我想在 python 源代码中找到列表理解,为此我尝试使用 Pygments,但它没有找到实现这一点的方法。

更具体地说,我想做一个识别所有可能的列表理解的函数。例如:

[x**2 for x in range(5)]

[x for x in vec if x >= 0]

[num for elem in vec for num in elem]

[str(round(pi, i)) for i in range(1, 6)]
Run Code Online (Sandbox Code Playgroud)

这个例子是从https://docs.python.org/2/tutorial/datastructs.html#list-comspirations获得的

使用正则表达式也是有效的解决方案。

谢谢

python list-comprehension pygments

6
推荐指数
2
解决办法
643
查看次数

标签 统计

list-comprehension ×1

pygments ×1

python ×1