开发者

How to give extra arguments for the Python itertools.ifilter function?

In python I have the following function:

开发者_运维百科
def is_a_nice_element(element, parameter):
    #do something
    return True or False

Now I would like to filter a list with this function as predicate, giving a fixed parameter. Python has the itertools.ifilter function, but I can't figure out how to pass the parameter. Is this possible? If not, how can I solve this problem?


I like functools.partial much better than lambda.

itertools.ifilter( partial(is_a_nice_element, parameter=X), iterable )


Wrap it in a lambda:

itertools.ifilter(lambda e: is_a_nice_element(e, 42), iterable)

42 is your extra argument, or whatever else you want it to be.


The solutions use lambda and functools.partial are quite correct and directly answer your question, but I don't think they are really the ideal solution.

Using filter/itertools.ifilter/map/itertools.imap with an anonymous function is prettymuch always going to be less clear than using a generator expression or list comprehension. For example, in this case I would write the generator expression

(item for item in iterable if is_a_nice_element(item, constant))

instead of using ifilter. In this case the ifilter solution is still fairly readable, but in many cases trying to go through lots of trouble to define just the right function is not going to be as worth while as just performing the operation.


If you are using ifilter then parameter would need to be constant in which case you could use a default argument parameter=something. If you want parameter to vary, you'd need to use another method to take a dyadic predicate.

If you already have the list in hand, ifilter is a bit of overkill relative to the built-in filter.

Describing what you want to accomplish would help in making a more concrete answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜