1

Is it possible to run such part of code in for cycle, please? How to change variables on the left?

A = np.loadtxt('f1.txt', unpack=True)
B = np.loadtxt('f2.txt', unpack=True)
C = np.loadtxt('f3.txt', unpack=True)
D = np.loadtxt('f4.txt', unpack=True)

I tried this, but it is not the right usage of eval function, right?

p = ['A', 'B', 'C', 'D']

#eval(p[0]) = np.loadtxt('f1.txt', unpack=True)

1 Answer 1

1

You can't assign to a variable using eval. You can, however, assign using the locals() dictionary -

locals()['a'] = np.loadtxt('f1.txt')

However, even that approach is dangerous as you are modifying the symbol table directly. The docs say:

Note The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.

You could try doing something like -

p = []
for i in range(1, 5):
    p.append(np.loadtxt(f'f{i}.txt')

#unpack subsequently
a, b, c, d = p
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.