python过滤列表中的所有负数_输出lis中的所有负数

本文介绍如何在Python中过滤列表,找出并返回所有的负数。通过遍历列表,当元素小于0时将其添加到新的列表中。最后返回包含所有负数的新列表。示例代码包括从用户输入获取数值,转换为浮点数列表,并打印负数列表及其数量或总和。
摘要由CSDN通过智能技术生成

因为函数只能返回一次。在The return statement terminates the execution of a function and

returns control to the calling function. Execution resumes in the

calling function at the point immediately following the call.

所以在第8行,您返回sum,然后就存在了。它不会进行循环的下一次迭代,因此它返回第一个num,这对于该条件是真的。在Solution:

将每个迭代结果存储在list、dict等位置,然后在函数的最后返回结果。在

你可以试试:#creat list using function

def Negative(List):

neg_num=[]

for i in List:

if i<0:

neg_num.append(i)

return neg_num

#input and print list

inputData= input('enter numbers ')

List = [float(i) for i in inputData.split(' ')]

print(Negative(List))

输出:

^{pr2}$

正如你所说:how to count the numbers obtained from the list??

试试这个:#creat list using function

def Negative(List):

neg_n

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值