itertools.ifilter Vs. filter Vs. list comprehensions

后端 未结 4 2011
孤城傲影
孤城傲影 2020-12-30 22:25

I am trying to become more familiar with the itertools module and have found a function called ifilter.

From what I understand, it filters

4条回答
  •  误落风尘
    2020-12-30 23:05

    Here, you can see the diference:

    filter(function, iterable): Construct a list from those elements of iterable for which function returns true.

    itertools.ifilter(predicate, iterable): Make an iterator that filters elements from iterable returning only those for which the predicate is True.

    This means that to obtain 'ifiltered' items you should iterate with returned iterator, but 'filter' returns all elements in a list with out iteration needed.

提交回复
热议问题