But why is that the case, and why do we need this guidance? Explanation We should expect that standard library code should be as performant and correct as possible. PEP 8, the official Python style guide for Python code in Python's standard library, asserts:įor sequences, (strings, lists, tuples), use the fact that empty sequences are false. It will test False if it is empty, and True otherwise. Place the list in a boolean context (for example, with an if or while statement). How do I check to see if a is empty? Short Answer: This will make the x.size check work in all cases I see on this page.įor example, if passed the following: a = We usually even just use the same name, as the conversion to an array won't make it back outside of the current scope: x = numpy.asarray(x, dtype=numpy.double) So it's very quick whenever it can be, and it ensures that you just get to assume the input is a NumPy array. This takes your input, does nothing if it's already an array, or wraps your input into an array if it's a list, tuple, etc., and optionally converts it to your chosen dtype. There are a few nice functions for doing this quickly - most importantly numpy.asarray. If you need to do more than just check if the input is empty, and you're using other NumPy features like indexing or math operations, it's probably more efficient (and certainly more common) to force the input to be a NumPy array. Not very "pythonic", but it turns out that NumPy intentionally broke pythonicity in at least this sense. If you're not sure whether it might be a list, a NumPy array, or something else, you could combine this approach with the answer gives to make sure the right test is used for each type. The numpythonic wayĪs explained in the SciPy FAQ, the correct method in all cases where you know you have a NumPy array is to use if x.size: > x = numpy.array() Returns 1, even though the array has zero elements. ), the if statement will incorrectly result in False: > x = numpy.array()īut clearly x exists and is not empty! This result is not what you wanted. However, if that one element happens to be 0 (or 0.0, or False. If you happen to have a NumPy array with exactly one element, the if statement will "work", in the sense that you don't get an error. Use a.any() or a.all()īut at least the case above tells you that it failed. ValueError: The truth value of an array with more than one element is ambiguous. But this doesn't make any sense, so you get a ValueError: > x = numpy.array() The "pythonic" way fails with NumPy arrays because NumPy tries to cast the array to an array of bools, and if x tries to evaluate all of those bools at once for some kind of aggregate truth value. I explain why below, but in short, the preferred method is to use size. You need to be careful with NumPy arrays, because other methods that work fine for lists or other standard containers fail for NumPy arrays. Other methods don't work for NumPy arrays This is the first google hit for "python test empty array" and similar queries, and other people are generalizing the question beyond just lists, so here's a caveat for a different type of sequence that a lot of people use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |