0

I have 10 lists and want to perform same process on all of them and like to have unique name of all variables. Here is what I have:

for i in xrange(1,11,1):
    m + str(i) = np.array(m + str(i)).astype(np.float)

It throws syntax error:

Can't assign to operator(mean.py,line30)
5
  • You can't assign to an arbitrary expression, only names (x) and indexed expressions (x[1] for lists, x['foo'] for dicts). Commented Oct 28, 2015 at 20:50
  • @Hackaholic: a variable name as I would like to save all lists with names m1, m2..... Commented Oct 28, 2015 at 20:52
  • 3
    Don't use multiple similarly named variables; use a single list m. Commented Oct 28, 2015 at 20:52
  • Or use a single multi-dimensional array. Depending on what your data looks like, that might be more appropriate. Commented Oct 28, 2015 at 20:56
  • Whenever you find yourself using sequentially numbered variables, you should probably be using an array/list instead. This is true for almost all programming languages. Commented Oct 28, 2015 at 21:02

1 Answer 1

1

Instead of m1 et al., use a single list m.

m = [ ... ]
for i in xrange(1,11,1):
    m[i] = np.array(m[i]).astype(np.float)
Sign up to request clarification or add additional context in comments.

5 Comments

at start list m is empty, so you might get list index out of range
m should be created as the collection of individual variables; m = [m1, m2, m3].
What is list + str(i) supposed to be? If you already have m1 et al. defined, you can literally just write m = [m1, m2, m3] to create the list. Ideally, though, you get rid of those variables and define m in terms of whatever you used to set the individual variables
Add an example of how you set the individual variables to your question.
I created m as list of lists and used above. It worked. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.