具有多个'if'的python列表理解 [英] python list comprehension with multiple 'if's

查看:28
本文介绍了具有多个'if'的python列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道python的

We all know python's

[f(x) for x in y if g(x)]

语法.

然而AST 列表理解的表示有不止一个'if' 表达式:

However the AST representation of list comprehension has room for more than one 'if' expression:

comprehension = (expr target, expr iter, expr* ifs)

谁能给我一个 python 代码的例子,它会产生一个 AST,其中包含多个if"表达式?

Can somebody give me an example of python code that would produce an AST with more than one 'if' expression?

推荐答案

将它们一个一个地堆叠起来:

Just stack them after one another:

[i for i in range(100) if i > 10 if i < 50]

生成 11 到 49 之间的整数,包括 11 和 49.

Produces the integers between 11 and 49, inclusive.

这篇关于具有多个'if'的python列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆