0

I am trying to alter the values in an array that I am working on.

This was my first try but the values did not change apparently

for x in  TimedA:
    x=TimedA[i]=Ihold+Iscale*x

and then I tried this:

for i in enumerate(TimedA):
TimedA[i]=Ihold+Iscale*TimedA[i]

but get this error:

TypeError                                 Traceback (most recent call last)
<ipython-input-159-62d5e5c1c721> in <module>
     18 #new stimulus with scaling
     19 for i in enumerate(TimedA):
---> 20     TimedA[i]=Ihold+Iscale*TimedA[i]
     21     #print(x)
     22 

TypeError: list indices must be integers or slices, not tuple

Can you explain to me what is going wrong?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tzikos
  • 111
  • 3
  • 10
  • 2
    You're nearly there. `enumerate()` returns `tuples`, so you'll need to either read in `for i, x in enumerate(TimedA)` or index the tuple for the first element to get the index. – TrebledJ Jan 04 '19 at 10:01
  • The error message tells you what is going wrong - you're using a tuple (`(index, element)`, which is what `enumerate` generates) as a list index. Try `for i, x in enumerate(TimedA): ...`. – jonrsharpe Jan 04 '19 at 10:02
  • Sometimes you want to use `print` to know what's going on. – LtWorf Jan 04 '19 at 10:18
  • Possible duplicate of [TypeError: list indices must be integers or slices, not tuple while doing some calculation in a nested list](https://stackoverflow.com/questions/51931817/typeerror-list-indices-must-be-integers-or-slices-not-tuple-while-doing-some-c) – Georgy Jan 04 '19 at 11:00

1 Answers1

2

If you inspect x in enumerate, you'll find:

for x in enumerate(['a', 'b', 'c']):
    print(type(x)) # tuple
    print(x)       # (0, 'a') ..

So actually what you want is:

for i, x in enumerate(TimedA):
    TimedA[i] = #...

Or you may use:

for i in range(len(TimedA)):
    TimedA[i] = #...

p.s. Variables in Python should be named with lowercase alphabets with underscore: timed_a.

See this naming convention.

knh190
  • 2,744
  • 1
  • 16
  • 30