python统计正数负数的个数
程序员文章站
2022-03-10 09:53:06
...
方法一:
#encoding=utf-8
original_list=[1,3,5,7,0,-1,-9,-4,-5,8]
positive_list=[]
negative_list=[]
for i in original_list:
if i>0:
positive_list.append(i)
elif i<0:
negative_list.append(i)
else:
print(str(i)+" is not positive or negative")
结果:
0 is not positive or negative
正数个数:5
负数个数:4
方法二:
#encoding=utf-8
positiveNum=0
negativeNum=0
nonePositiveNegative=0
for i in original_list:
if i>0:
positiveNum=positiveNum+1
elif i<0:
negativeNum=negativeNum+1
else:
nonePositiveNegative=nonePositiveNegative+1
print "正数个数:"+str(positiveNum)
print "负数个数:"+str(negativeNum)
print "既不是正数也不是负数的个数:"+str(nonePositiveNegative)
输出结果:
正数个数:5
负数个数:4
既不是正数也不是负数的个数:1