[英]Is there a way to check if any of list elements are in a string and replace them?
[英]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.