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

查看:70
本文介绍了带有多个'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代码示例,该示例会产生带有多个'if'表达式的AST吗?

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

推荐答案

只需将它们彼此堆叠:

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

产生11到49之间的整数.

Produces the integers between 11 and 49, inclusive.

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

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