I'm stunned by the behavior of filter () due to different versions of Python


Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> data = 'spam123'
>>> bool(filter(lambda s:s.isupper(),data))
False
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> data = 'spam123'
>>> bool(filter(lambda s:s.isupper(),data))
True

If anyone is familiar with it, please explain it.

You explained it !! 2014-02-15 10:10

http://qiita.com/trsqxyz/items/50e6e59e23995b6c2b41#comment-d01210d7bd8f60d2c590 It seems that the reason why filter () returned the list in the 2nd system is that the 3rd system now returns the iterator.

@shiena Thank you! !!

The Boolean value has changed because the empty list has changed to an iterator object. Dive into Python 3 seems to be useful here. So


Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> data = 'spam123'
>>> bool(list(filter(lambda s:s.isupper(),data)))
False

have become It's important to verify the functions one by one

You commented !! 2014-06-01 10:46

http://qiita.com/trsqxyz/items/50e6e59e23995b6c2b41#comment-945bff027756ad382a35 If you want to write something like a code example, any is better than bool.

ʻAny () is a built-in function that returns Trueif any of the iterable elements are true. For all elements, there is ʻall (). Certainly it seems more appropriate to use ʻany () `. Thank you very much.

ʻAny () and bool () `

>>> a=[0]
>>> bool(a)
True
>>> any(a)
False

ʻAny ()checks for each element Doesbool ()` feel like there is an element?

Recommended Posts

I'm stunned by the behavior of filter () due to different versions of Python
Debug by attaching to the Python process of the SSH destination
Behavior of python3 by Sakura's server
[python] A note that started to understand the behavior of matplotlib.pyplot
Pandas of the beginner, by the beginner, for the beginner [Python]
I tried to verify and analyze the acceleration of Python by Cython
The inaccuracy of Tensorflow was due to log (0)
The answer of "1/2" is different between python2 and 3
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[python] How to sort by the Nth Mth element of a multidimensional array
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
How to erase the characters output by Python
About the behavior of Model.get_or_create () of peewee in Python
Let's use different versions of SQLite3 from Python3!
Add a function to tell the weather of today to slack bot (made by python)
Easy way to check the source of Python modules
python beginners tried to predict the number of criminals
The wall of changing the Django service from Python 2.7 to Python 3
Template of python script to read the contents of the file
How to get the number of digits in Python
List of posts related to optimization by Python to docker
[python] option to turn off the output of click.progressbar
Indent behavior of json.dumps is different between python2 and python3
Read the xml file by referring to the Python tutorial
I checked out the versions of Blender and Python
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
[Python] Summary of how to specify the color of the figure
14 quizzes to understand the surprisingly confusing scope of Python
[Introduction to Python] Basic usage of the library matplotlib
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Completely translated the site of "The Hitchhiker's Guide to Python"
Python Note: The mystery of assigning a variable to a variable
I tried to summarize the string operations of Python
[python] behavior of argmax
the zen of Python
How to check if the contents of the dictionary are the same in Python by hash value
Try to import to the database by manipulating ShapeFile of national land numerical information with Python
I tried to find the entropy of the image with python
Try to get the function list of Python> os package
Reuse the behavior of the @property method by using a descriptor [16/100]
[Python] How to make a list of character strings character by character
How to switch the configuration file to be read by Python
How to test the attributes added by add_request_method of pyramid
Make the display of Python module exceptions easier to understand
The story of introducing jedi (python auto-completion package) to emacs
Tank game made with python About the behavior of tanks
Different from the import type of python. from A import B meaning
[Python3] Call by dynamically specifying the keyword argument of the function
[Python] I tried to visualize the follow relationship of Twitter
Try to automate the operation of network devices with Python
I want to know the features of Python and pip
Find the diameter of the graph by breadth-first search (Python memory)
Get the source of the page to load infinitely with python.
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
When you want to use multiple versions of the same Python library (virtual environment using venv)
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
I tried to summarize the contents of each package saved by Python pip in one line
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
Towards the retirement of Python2
About the ease of Python