0

I was wondering how I could name a matrix variable.

Let's say the matrix a

var=din3
a
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

I want to concatenate the name of the variable with a string.

I want to do something like this: a+"IM"+var

so that the resulting variable would be aIMdin3. So when I call aIMdin3 I get the matrix, instead of calling a.

How could I do this?

hsayya
  • 131
  • 1
  • 10
  • 1
    Dynamic naming of variables is a bad idea. You can get just about all the benefits without the problems by using a dictionary. Instead of using a variable name you rather use a dictionary key. So instead of using `alMdin3` you use `mydict['alMdin3']`. – Rory Daulton Jan 28 '19 at 01:19

1 Answers1

0

There is a way to dynamically create variable by playing with globals(), but I wouldn't recommend this type of "meta-programming" In my humble opinion, it's best to use dictionary instead and generating your dynamic variables by creating dynamic keys to your dictionary.

var = "din3"
d = {}
d['aIM'+var] = a

For more details, visit How can you dynamically create variables via a while loop?

Rags
  • 21
  • 3