简体   繁体   English

如何编写 Inline/Lambda 以追加或不追加到列表

[英]How to write Inline/Lambda for appending or not appending to a list

I feel this a noob question but I haven't used lambda functions that much and I couldn't find what I needed online.我觉得这是一个菜鸟问题,但我没有太多地使用 lambda 功能,而且我在网上找不到我需要的东西。 I wanted to write a lambda function which takes a series as an input, and returns a list without any 'None's in it Basically I want a lambda function for the followinf function: I wanted to write a lambda function which takes a series as an input, and returns a list without any 'None's in it Basically I want a lambda function for the followinf function:

def get_path(x):
  toRet = []
  for item in x.tolist():
    if item is not None:
      toret.append(item)
  return toRet

Is it possible to get an inline lambda function which does what get_path() does.是否有可能得到一个内联 lambda function 做 get_path() 所做的事情。 Yes I do know that I can do the following:是的,我知道我可以执行以下操作:

lambda x: get_path(x)

It solves the problem but I would really love to know how to make an inline function for this.它解决了这个问题,但我真的很想知道如何为此制作内联 function。

get_path = lambda x: [ elem for elem in x.tolist() if not elem]

You don't even need lambda functions here.您甚至不需要 lambda 函数。

toRet = [item for item in x.tolist() if item is not None]

You can also use filter for example, which looks neater例如,您还可以使用filter ,它看起来更整洁

toRet = list(filter(None, x.tolist()))

As others have pointed out, you can use filter() but be careful of what function you provide for the filter-check.正如其他人指出的那样,您可以使用filter()但请注意您为过滤检查提供的 function 。

If function is None , the identity function is assumed, that is, all elements of iterable that are false are removed.如果functionNone ,则假定身份 function ,即所有为 false 的iterable元素都将被删除。

list(filter(None, something)) won't work correctly because it will filter out False-ish values too, not just None . list(filter(None, something))将无法正常工作,因为它也会过滤掉 False-ish 值,而不仅仅是None Example:例子:

>>> mylist = [1, 2, 3, 0, None, '', tuple(), 'last']
>>> list(filter(None, mylist))  # not the expected behaviour
[1, 2, 3, 'last']

OP had an explicit if item is not None check, rather than just if item - so the two are not equivalent. OP 有一个明确的if item is not None检查,而不仅仅是if item - 所以两者不相等。 Only None needs to be filtered out.只有None需要被过滤掉。 To get the correct filtering behaviour, provide an actual check for the function provided to filter:要获得正确的过滤行为,请对提供给过滤器的 function 进行实际检查:

>>> list(filter(lambda i: i is not None, mylist))
[1, 2, 3, 0, '', (), 'last']

Replace mylist with x.tolist() .mylist替换为x.tolist() And when you put that into a lambda, it gets messy:当你把它放入 lambda 时,它会变得一团糟:

get_path = lambda x: list(filter(lambda i: i is not None, x.tolist()))

Instead of all that, the list comprehension option is better:而不是所有这些,列表理解选项更好:

get_path = lambda x: [i for i in x.tolist() if i is not None]

with an explicit is not None check.带有明确is not None检查。

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

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