How to map an array with user defined variable array in python to perform some operation?

I have an array of dimension 3x3, I would like to first map this array on user define variable of same dimension. Following, I provided an example, where I want to create direct correspondence between a and b . So for each row I can perform some operation:

a=[[11, 2, 7], [5, 7, 8], [4,2,1]]
b= [[pm20, pm30, pm21],[pm21, pm30, pm20],[pm21, pm20, pm30]]

For each row, I want to perform following operations:

x= pm20 - pm21
y= pm30 - pm21

(I am new to python)

Thanks in advance

You have some conflicting so its not clear to me exactly what’s going on.

But with variables you can do something like this

a = [[11, 2, 7], [5, 7, 8], [4, 2, 1]]
[[pm20, pm30, pm21], [pm21, pm30, pm20], [pm21, pm20, pm30]] = a

print(a)
print(pm20)

For operating on rows, you can use the syntax

for row in a:
  # Do stuff
1 Like

Hi JeremyLT,
Thank you very much for quick response, when I perform following following operation,
for row in a:
xx=pm20+pm21
print(xx)
I get something,
6
6
6
But I was expecting,
18
13
6

I think, its operating on last row, probably I am doing something wrong.
Thanks again,

If you need the values of pm20 and pm21 to differ by row, then you will need to set them in the loop too.

for row in a:
    [pm20, pm30, pm21] = row
    # Do stuff

Though, I’m not sure how you want to account for the fact that your locations move around.

1 Like

Hi JeremyLT,
Thank you very much, its working, have a nice day.

1 Like

Hi JeremyLT,
Sorry to bother you again, I just realized a problems in your solution,

[pm20, pm30, pm21] = row

which isn’t iterate for each row. The answer I was expecting:
18
13
6
but I am getting
18
13
5
So, it means its adding according to the arrangement given in row.

As for my case, row arrangement is changing. I couldn’t solve this problem.

Thank you very much for your time.

That’s what I meant by this. You could store the data consistently. Or you could index into each row according to the pattern

for i in range(3):
  x = a[I][(0+i)%3] - a[I][(2+i)%3]
1 Like

Hi JeremyLT,
As from below, I can certainly change the location in each row.

for i in range(3):
x = a[i][(0+i)%3] - a[i][(2+i)%3]
print(x)

But in my case, there is direct correspondence of element between a and variables (pm20 etc…) .
For example for first row:

pm20=11
pm30=2
pm21=7

for second row,

pm21=5
pm30=7
pm20=8

similarly, for third row, I want to do a below operation in each row considering the fact that I dont know the exact position or index number of variables.

x= pm20 - pm21
y= pm30 - pm21

Actually, my matrix have huge dimension (132x64) and there are several variables whose changing index in each row. So, my goal is to map exactly variable to corresponding values. So when I call pm30 or pm21, for each row the program will find and do operation and goes to second row.

Thank again for your kind replies.

If you don’t know which values in each row you need, then there is no way to do this without hard coding the exact location of each variable.

1 Like

Thank you very much your time.

Sorry I couldn’t be more helpful. Hopefully whoever is writing the data you are working with can output it in some pattern so that you can read and process it. Maybe they can store it in key value pairs, with a dictionary, so that you can make sense of it.

1 Like

Hi again JeremyLT,
After carefully thinking what you suggested above, I come up with following:

import numpy as np

a = np.genfromtxt(‘Values.txt’)

L = [[‘pm20’, ‘pm30’, ‘pm21’], [‘pm21’, ‘pm30’, ‘pm20’], [‘pm21’, ‘pm20’, ‘pm30’]]

L1 = np.array(L)
Ind1 = np.where(L1[1] == ‘pm20’)
Ind2 = np.where(L1[1] == ‘pm21’)
print(a, Ind1, Ind2)

for i in range(3):
Ind1 = np.where(L1[i] == ‘pm20’)
Ind2 = np.where(L1[i] == ‘pm21’)

x = a[i][(Ind1[0])%3] + a[i][(Ind2[0])%3]
print(x)

where a is:

a=[[11, 2, 7], [5, 7, 8], [4,2,1]]

Now my question is, is there a way I can also import L like a :

[[pm20, pm30, pm21],[pm21, pm30, pm20],[pm21, pm20, pm30]]

So, I don´t need to define within a code.

Thank you very much for your help.