0

i wass trying to print the Training result, However, the test accuracy can not be sumed.

q=(['0.50000', '0.56250', '0.50000', '0.50000'])

sum(q)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
上官林源
  • 19
  • 1
  • 2

5 Answers5

5

You have a list of str so first you have to convert them to float, which you can do using a generator expression within sum.

>>> sum(float(i) for i in q)
2.0625
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

Someone should post the imho proper version (see comments below):

>>> sum(map(float, q))
2.0625
superb rain
  • 5,300
  • 2
  • 11
  • 25
  • 1
    Declaring that `map` is the "proper" version over a generator expression is somewhere between tenuous and flat-out wrong https://stackoverflow.com/questions/1247486/list-comprehension-vs-map – Cory Kramer Aug 26 '20 at 11:43
  • @CoryKramer Well imho, reimplementing a built-in isn't a proper thing to do. Not sure what that very old answer about a different case and rather agreeing with me is supposed to tell me? – superb rain Aug 26 '20 at 11:51
  • @CoryKramer Can you please enlighten me about what that answer is supposed to tell me and how I'm between tenuous and flat-out wrong? – superb rain Aug 26 '20 at 12:16
  • Well I guess that depends on why your version is "proper" in the first place. If it is related to performance, `map` vs generator expressions are essentially equal in speed depending on the use-case. If it about style, most (?) python developers consider generator expressions "more pythonic". At the end of the day, it really doesn't matter and is up to each developer which they prefer. And since it is indeed a preference at that point, neither solution is more "proper" than the other. – Cory Kramer Aug 26 '20 at 12:20
  • @CoryKramer Like I said, I don't find it proper to reimplement a built-in that Python already provides. And I wouldn't call the speed "essentially equal". I tested our solutions on lists from 0 to 1,000,000 elements, mine was consistently faster by about factor 1.23. Where do I find statistics about how many Python developers consider that generator expression solution more pythonic? – superb rain Aug 26 '20 at 15:54
0

sum function uses start value 0

>>> help(sum)
Help on built-in function sum in module builtins:

sum(iterable, /, start=0)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers

    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.

So adding a int object with a string object will raise TypeError

>>> 0 + '0.50000'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In order to fix this you can convert the string object into float object first and then apply the sum function.

Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
0

you can do it like this:

q=(['0.50000', '0.56250', '0.50000', '0.50000'])
result = 0 # create a variable wich will store the value.

for i in q: # loop over your elements
    result += float(i) # cast your temp variable (i) to float and add each element to result. 
print(result) # escape the loop and print the result variable.
The.B
  • 361
  • 2
  • 11
0
q=([0.50000, 0.56250, 0.50000, 0.50000])
sum(q)

or

q=(['0.50000', '0.56250', '0.50000', '0.50000'])
sum([float(x) for x in q])
Ritesh
  • 15
  • 7
  • Explaining what, exactly, was wrong with the question and how your answer solves the problem would be important. – SolarBear Aug 26 '20 at 12:19