Python中列表中浮点数的总和 [英] Sum of float numbers in a list in Python
本文介绍了Python中列表中浮点数的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 s
中有浮点值:
p = list(swn.senti_synsets(a))
s = p[0].pos_score()
print(s)
# Output
0.0
0.0
1.0
0.0
0.25
0.25
然后我试了一下,print(sum(s))
这给出了错误浮动"对象不是可迭代的.这该怎么做 ?
then I tried, print(sum(s))
which gives the error 'float' object is not Iterable.
how to do this ?
解决办法:奇怪的是我自己找到了答案,我不知道,但是把一个单独的函数的东西放在一边.`
Solution: Strange that I found the answer myself, i dont know but putting the thing of a separate function worked. `
for x in token:
sum_pos=sum_pos+posCheck(x)
sum_neg=sum_neg+negCheck(x)
def posCheck(a):
p=list(swn.senti_synsets(a))
s = p[0].pos_score() return(s)`
def negCheck(a): p=list(swn.senti_synsets(a)) s = p[0].neg_score() return(s)
我无法总结列表,但是当我将函数放入 returntype 时,它返回了正数的总和.感谢大家的帮助.
I couldn't sum up the list, but when I put the function with returntype, it returned the sum of the positive numbers. Thanks to all of you for trying to help.
推荐答案
values = [0.0, 0.0, 1.0, 0.0, 0.25, 0.25]
print sum(values)
对我来说很好
这篇关于Python中列表中浮点数的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文