简体   繁体   中英

Is there any way to check if there are negative elements on the list and if so remove them?

I have these lines here and I want to write a program that checks if there is a negative number in the list,and if so, remove it.Then, calculate the average of the 2 first elements Can someone help?

def test() : 
  values = [3, 6, 5, 4, -5]

print(values) return values

Just check if a number is less than 0 :

def test(values):
    l = [n for n in values if n >= 0]
    return l, (l[0]+l[1])/2

print(test([3, 6, 5, 4, -5]))

Output:

[3, 6, 5, 4], 4.5

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