简体   繁体   English

在列表理解/映射/过滤器中分配标识符

[英]Assign identifier inside list comprehension/ mapping/ filter

I want to pass a lambda expression to a filter that captures an outside variable l . 我想将lambda表达式传递给捕获外部变量l的过滤器。 I want l to be the list passed to filter. 我希望l成为传递给过滤器的清单。 Assume that that list comes out of some other list comprehension/ mapping/ filtering. 假设该列表来自其他一些列表理解/映射/过滤。 Is it possible to assign an identifier (in tis case l ) to that list? 是否可以为该列表分配标识符(在情况l )? Like so: 像这样:

filter((lambda x : len([z for z in l if z == x]) == 1), l@[1,1,2,3,4,4,5,6,6] )

I just used @ because in Haskell you can use @ in a similar way. 我只是使用@因为在Haskell中您可以以类似方式使用@

Is there some succinct syntax for this or do I need to break up the operation into several lines and assign l in a normal manner? 是否有一些简洁的语法,或者我需要将操作分成几行并以正常方式分配l

It is not possible to give a name 'on the fly' to the second parameter of filter . 不能给filter的第二个参数“即时”命名。

If even it were, I think that Python code should be more explicit (see " The Zen of Python ") and I suggest to split the code in two or more lines. 如果是这样,我认为Python代码应该更明确(请参阅“ Python之禅 ”),我建议将代码分成两行或更多行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM