How to make an associative matrix in python 3?

Asked

Viewed 123 times

1

I have a code that asks for a matrix 8x8, but I need in the input He asks for the values like this:

matriz [A][1]: 

matriz [A][2]:

That is, he asks for the line in letter form and the column in number form.

So far I’ve only been able to get him to show up the row and column in number form, like this:

matriz [1][1]:

matriz [1][2]:
  • 1

    Could [Edit] the question and add the code it has?

2 answers

0


python has the function Chr, it returns a character from an integer. For the uppercase alphabet, this number starts with 65 (A) and ends with 90 (Z)).

>>> chr(65)
   'A'
>>> chr(90)
   'Z'

The opposite of this function is the Ord.

>>> ord('A')
   65
>>> chr('Z')
   90

Printable matriz [A][1]: from a i=0 and j=0, just do:

print('matriz [{}][{}]: '.format(chr(65+i),j+1))

  • thanks!!! I got by this method

  • 1

    @Letíciagabrielacena, if the answer was satisfactory for you mark it as accepted. So you and the author of the answer earn points :)

0

You can work directly with a dictionary, ie:

matriz_assoc = {
        "a": [ 1, 2, 3, ],
        "b": [ 4, 5, 6, ],
        "c": [ 7, 8, 9, ]
    }

And to access your content you will use:

print(matriz_assoc["a"][0])
1

Of course, in memory Python will not keep the order of your dictionary items, so to print it, use something like this:

for linha in sorted(matriz_assoc.keys()):
    print(matriz_assoc[linha])

To generate an output in the correct order.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.