繁体   English   中英

有什么方法可以检查列表中是否有负面元素,如果有则删除它们?

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

我在这里有这些行,我想编写一个程序来检查列表中是否有负数,如果有,请将其删除。然后,计算前两个元素的平均值有人可以帮忙吗?

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

打印(值)返回值

只需检查一个数字是否小于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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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