Why does Pythons `any` function not return True or False? [closed]

2024/11/15 13:26:28

If I try

>>> from pylab import *
>>> b = [2, 3, 4, 5, 6, 7]
>>> a = any(x < 0 for x in b)  
>>> print(a)

it doesn't return True or False.

It returns

<generator object <genexpr> at 0x7fbd62129ab0>
Answer

You are using a numpy.any() instead of the built-in any(). Most probably you have from numpy import any or from numpy import *, which causes this behavior.

Why that happens?

According to the documentation, any tests if any element evaluates the condition. However, if you look into the source code, it actually returns a asanarray() result which is a generator.

How to avoid it?

It is always a good idea to import only scope rather than the method itself, like so: import numpy as np

:)

UPDATE 1

Personally, I have never used iPython, but thanks to comments by @Praveen and @hpaulj, if you use --pylab flag with ipython, you will see the same behavior, and you can turn that behavior off - never knew it! :)))

https://en.xdnf.cn/q/120444.html

Related Q&A

Write a list of lists into csv in Python [duplicate]

This question already has answers here:Writing a Python list of lists to a csv file(12 answers)Closed 5 years ago.I have a list of lists of pure data like thisa=[[1,2,3],[4,5,6],[7,8,9]]How can I write…

Upper limit of points pyplot

Just being a curious George here. But Im handling 279 million data points (x,y) and am wondering if pyplot can scatter such a number?Thanks.

how to delete the u and before the of database table display by python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

python list permutations [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:How to generate all permutations of a list in Python I am given a list [1,2,3] and the task is to create all the possible …

Repeating Characters in the Middle of a String

Here is the problem I am trying to solve but having trouble solving:Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a n…

Remove a big list of of special characters [duplicate]

This question already has answers here:Remove specific characters from a string in Python(27 answers)Closed 5 years ago.I want to remove each of the following special characters from my documents: symb…

How to change a html page from flask to Django [duplicate]

This question already has an answer here:How to specify URLs in Django templates?(1 answer)Closed 7 years ago.I am working on an app that requires changing a flask template to that of Django.How to ch…

How to get the text of Checkbuttons?

Checkbuttons gets generated dynamically and they are getting text from a python list. I need a logic for capturing selected checkbuttons text . As per my research everywhere they are returning the stat…

Working out an equation

Im trying to solve a differential equation numerically, and am writing an equation that will give me an array of the solution to each time point.import numpy as np import matplotlib.pylab as pltpi=np.p…

combine rows and add up value in dataframe

I got a dataframe(named table) with 6 columns labeled as [price1,price2,price3,time,type,volume]for type, I got Q and T, arranged like:QTQTTQNow I want to combine the rows with consecutive T and add up…