简体   繁体   中英

How to I detect if each number in the list is equal to or below zero?

在此处输入图像描述

I have -1 as each element in the list but it still prints "there is a positive"

Thanks in advance,

WG

all() checks if all items in an array are True, then returns a boolean. To check if a number is less than zero in the list, you can do: all([i>=0 for i in [-1,-2,-3,3]])

I would just do

if max(test_3) <= 0:
    print("all negative")
else:
    print("there is a positive")

This seems more explicit to me that you want the maximum value to be 0 or less

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