Home > Software design >  Array index for an ordered pair
Array index for an ordered pair

Time:03-24

Hi I need create a ordered pair using this logic (no functions):

#---------------------------------------------------------------------------------

A = [2,5,7];
B = [6,2];
LA = len(A)
LB = len(B)
Maux = [];
print(type(Maux))

for i in range(0,LA,1):
    for j in range(0,LB,1):
        
        Maux[i-1*LB   j , j] = [A[i] , B[j]] 

print(Maux);

#---------------------------------------------------------------------------------

When I compile the system says: list indices must be integers or slices, not tuple

I read about this problem and I think is an incorrect index to access the matrix.

Any idea?

CodePudding user response:

If you declare Maux to be a dict your code will work: initialize it with Maux = {}.

Also, get rid of the semicolons at line-ends: you're writing Python, not C Java Pascal etc.

You don't need ",1" at the end of range(...) calls, commas should be followed by single spaces for readability, you don't really need LA but it's ok, and you don't need to multiply anything by 1. With these improvements:

A = [2, 5, 7]
B = [6, 2]
LA = len(A)
LB = len(B)
Maux = {}
print(type(Maux))

for i in range(0, LA):
    for j in range(0, LB):
    
        Maux[i - LB   j, j] = [A[i] , B[j]] 

print(Maux)

Output:

<class 'dict'>
{(-2, 0): [2, 6], (-1, 1): [2, 2], (-1, 0): [5, 6], (0, 1): [5, 2], (0, 0): [7, 6], (1, 1): [7, 2]}
  • Related