简体   繁体   中英

How to check if any element is None, or empty array, or False in a python iterable array (excluding zeros)?

There is a list:

x = [5, "ce", 0, (32, "a")]

It doesn't contain a None, or empty array, or False element, so it should return True .

0 shouldn't be counted as an empty object.

y = [5, "ce", 0,, "", (32, "a")]

It contains an empty string so it should return False .

How would you do it in the fastest way?

Use the built-in all :

>>> all(e not in [None, []] and e is not False for e in [5, "ce", 0, (32, "a")])
True
>>> all(e not in [None, []] and e is not False for e in [5, "ce", 0, 0, (32, "a")])
False

I noticed that there was a problem using e not in [None, [], False] because 0 in [None, [], False] was giving True .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM