我試着使用匿名函數filter()
的許多不同的例子,但只要我在字符串上使用它,總會得到奇怪的結果。下面是一個例子:是否在Python 2.x和3.x之間更改了`filter()`?
>>>print(filter(lambda x: x.isdigit(), "aas30dsa20"))
<filter object at 0x00000000035DE470>
如果不是字符串,一切工作正常。例如;
>>> print(list(filter(lambda x: x >= 30 and x <= 70, [x**2 for x in range(1,11)])))
[36, 49, 64]
順便說一句,如果我刪除list()
功能部分,會出現類似於字符串大小寫問題:
>>> print(filter(lambda x: x >= 30 and x <= 70, [x**2 for x in range(1,11)]))
<filter object at 0x00000000037BFDD8>
我使用Python 3.4.1在Windows 7
總之:[是](https://docs.python.org/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists)。 – jonrsharpe