python2和python3中filter函数

2023-02-17,,,

python2python3filter是不同的,其中在python2中filter返回的是一个list,可以直接使用

>>> a = [1,2,3,4,5,6,7]
>>> filter(lambda x: x%2==0, a)
[2, 4, 6]

而在python3中,返回的是<filter object at 0x05D25D90>,应将filter转换成list,才能继续使用

>>> list(filter(lambda x:x*2, a))
[1, 2, 3, 4, 5]

python2和python3中filter函数的相关教程结束。

《python2和python3中filter函数.doc》

下载本文的Word格式文档,以方便收藏与打印。