1

I have the code below in Python3:

  matrix = np.array([[1,2,3],
                     [2,3,4],
                     [5,3,4]])

  sig_d1 = np.array([[1, 0, 0],
                    [0, 0, -1],
                    [0, -1, 0]])


 sig_d2 = np.array([[1, 0, 0],
                    [0, 0, 1],
                    [0, 1, 0]])

 for i in range(1,3):
     product=np.dot(sig_d+i,matrix)

However, I'm getting the error:

product = np.dot(sig_d+i,matrix.transpose())
     NameError: name 'sig_d' is not defined

Could someone give some support?

cs95
  • 379,657
  • 97
  • 704
  • 746

1 Answers1

2

Python does not allow you to dynamically access variables the way you're trying to do so. The best way to do this would be to put your arrays inside a dictionary and access values by key string.

array_dict = {'sig_d1' : sig_d1, 'sig_d2' : sig_d2}

for i in range(1,3):
    product = np.dot(array_dict['sig_d{}'.format(i)], matrix)

If you have more arrays, I'd recommend a smarter way of initialising your array_dict, possibly through a loop or dict comprehension.

cs95
  • 379,657
  • 97
  • 704
  • 746