0

probably a very simple question: How to change a list name in the for loop 'header'? For example, in the next piece of code the number '4' comes from a user defined variable 'N', so instead of looking like this:

for tr in st_**4**:
    tr.data = np.require(tr.data, dtype=np.float32)

I would like to establish the dependency with 'N', for example:

for tr in st_**N**:
    tr.data = np.require(tr.data, dtype=np.float32)

but I can not find what is the right way to write this.

thanks in advance.

user2390182
  • 72,016
  • 6
  • 67
  • 89
Pahan
  • 5
  • 2
  • 1
    Don't. This is a bad pattern. Use a dictionary (or list) to hold your variables, and access the values by key or position. – mozway Apr 24 '23 at 11:17
  • 1
    `lst = [str_1, str_2, …] ; for tr in lst: ...` or `d = {'str_3': …, 'str_4': …} ; for tr in range(1, 5): d[f'str_{N}'].data = …` as a general outline – mozway Apr 24 '23 at 11:20

0 Answers0