Python 3, module 'itertools' has no attribute 'ifilter'

时间秒杀一切 提交于 2019-12-10 12:43:24

问题


I am new at Python, trying to build an old python file into Python 3. I got several build errors which I solved. But at this point I am getting above error. I have no idea how to fix this. The code section looks like below.

return itertools.ifilter(lambda i: i.state == "IS", self.storage)

回答1:


itertools.ifilter() was removed in Python 3 because the built-in filter() function provides the same functionality now.

If you need to write code that can run in both Python 2 and Python 3, use imports from the future_builtins module (only in Python 2, so use a try...except ImportError: guard):

try:
    # Python 2
    from future_builtins import filter
except ImportError:
    # Python 3
    pass

return filter(lambda i: i.state == "IS", self.storage)


来源:https://stackoverflow.com/questions/33715579/python-3-module-itertools-has-no-attribute-ifilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!