I have got float values in 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
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)
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.
Use the list. append() method to add a float to a list in Python, e.g. my_list. append(3.3) . The list.
Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.
Use sum() to approach common summation problems. Use appropriate values for the iterable and start arguments in sum() Decide between sum() and alternative tools to sum and concatenate objects.
You can also use:
>>> l=[0.0, 0.0, 1.0, 0.0, 0.25, 0.25]
>>> sum(map(float,l))
1.5
As other said, sum(l)
will also work. I don't know why you are getting error with that.
One possible reason might be that your list is of string data type. Convert it to float as:
l = map(float, l)
or
l = [float(i) for i in l]
Then using sum(l)
would work properly.
EDIT: You can convert the s
into list and then sum it.
s = p[0].pos_score()
print sum(list(s))
To sum float from a list , one easy way is to use fsum
import math
l=[0.0, 0.0, 1.0, 0.0, 0.25, 0.25]
math.fsum(l)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With