列表推导中是否有类似于as
关键字的内容?
示例:代替
L = [foo(bar(baz(bla(x)))) for x in X if foo(bar(baz(bla(x)))) == 1]
这将是:
L = [foo(bar(baz(bla(x)))) as y for x in X if y == 1]
列表推导中是否有类似于as
关键字的内容?
示例:代替
L = [foo(bar(baz(bla(x)))) for x in X if foo(bar(baz(bla(x)))) == 1]
这将是:
L = [foo(bar(baz(bla(x)))) as y for x in X if y == 1]
在 python 3.8 中,您可以使用海象运算符来执行此操作:
>>> L = [y for x in X if (y := foo(bar(baz(bla(x))))) == 1]