本文介绍了Python 3,模块"itertools"没有属性"ifilter"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,试图将一个旧的python文件构建到Python 3中.我遇到了几个解决的错误.但是在这一点上,我正在克服错误.我不知道该如何解决.代码部分如下所示.

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)

推荐答案

itertools.ifilter()在Python 3中被删除,因为内置的 filter()函数现在提供了相同的功能.

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

如果您需要编写可在Python 2和Python 3中运行的代码,请使用 future_builtins模块(仅在Python 2中,因此请使用try...except ImportError:防护):

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)

这篇关于Python 3,模块"itertools"没有属性"ifilter"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:31
查看更多