-1

The problem I'm having is attempting to take the average of my list (derived from y, which is a list of sin values). However, when running the code, I get the error

TypeError: float() argument must be a string or a number, not 'list'

Any help you could offer would be greatly appreciated

for k in range(len(y)-1-r):

    list_to_avg = [y[r+k:len(y)-1-r+k]]
    b =float(sum(list_to_avg, []))
    a =float(len(list_to_avg))
    z.append(b/a)
D. Powell
  • 3
  • 1
  • 2
    helpful also to provide example *inputs*. Please take the [tour] and read [ask] for tips on improving the quality of this question. As currently asked, it's not very good. – David Zemens Feb 08 '18 at 16:27
  • Can't tell for sure without more info but I believe your error is on this line `list_to_avg = [y[r+k:len(y)-1-r+k]]`. Try removing the outer square brackets. You're getting a list of lists as it's currently written. It would also help to print intermediate variables when debugging. – pault Feb 08 '18 at 16:28

2 Answers2

0

You have made list_to_avg a list that contains a list.

Use

list_to_avg = y[r+k:len(y)-1-r+k]

instead.

David Jones
  • 4,766
  • 3
  • 32
  • 45
0

Change this:

b = float(sum(list_to_avg, []))

to this:

b = float(sum(list_to_avg))

You are asking sum to add an empty list ([]) to whatever the contents of list_to_avg are.


Since line b = float(sum(list_to_avg, [])) is not throwing:

TypeError: can only concatenate list (not "whatever1") to list

it means that list_to_avg is a list of lists. I cannot know whether that is intentional or not so I assume it is. But if it is, the result of the summation would also be a list which of course cannot be cast to float and thus the:

TypeError: float() argument must be a string or a number, not 'list'


1The whatever is mine. For example if you had sum([1, 2, 3], []) it would say TypeError: can only concatenate list (not "int") to list

Ma0
  • 15,057
  • 4
  • 35
  • 65