Skip to content

Instantly share code, notes, and snippets.

@kashewnuts
Created August 2, 2018 14:26
Show Gist options
  • Save kashewnuts/dee42b63a6584c6d3c4dace03fae45ef to your computer and use it in GitHub Desktop.
Save kashewnuts/dee42b63a6584c6d3c4dace03fae45ef to your computer and use it in GitHub Desktop.
Python2と3のfilterの違い
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [-1, 3, -5, 7, -9]
>>> filter(lambda x: abs(x) > 5, a)
[7, -9]
>>>
Python 3.6.5 (default, Apr  1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [-1, 3, -5, 7, -9]
>>> filter(lambda x: abs(x) > 5, a)
<filter object at 0x7f7c5853e7f0>
>>> filter(lambda x: abs(x) > 5, a)
<filter object at 0x7f7c5853e940>
>>> hoge =filter(lambda x: abs(x) > 5, a)
>>> hoge
<filter object at 0x7f7c5853e978>
>>> list(hoge)
[7, -9]
>>> hoge
<filter object at 0x7f7c5853e978>
>>> list(hoge)
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment