I am trying to convert a 2d array with really specific property to a dictionary. The property is that the value inside a matrix, corresponds to possibility to move in direction (upwards, downwards, left, right).

I have been trying to flatten it and then work on the new array, but indexing gets really nested. I have 2 questions: How to add keys and values to a dictionary? And may I ask if I am going on right direction?

For now, I assume that I am doing something such as:

# assuming the matrix to be:
# [[2, 1, 1],
#  [1, 2, 2],
#  [2, 1, 2]]
# so for instace if I am on coordinate on the upper row in the middle,
# I can move 1 position to the left, one position to right, and one pos down.
# I made this simple example to show what I am trying to do
b = [[2, 1, 1], [1, 2, 2], [2, 1, 2]]
a = make_flat_arr(b)
print(a)
position = [str((len(b))**2)] * (len(b))**2
for j in range(len(b)**2):
    position[j] = 'Pos{' + str(j % len(b)) + '}{' + str(j // len(b)) + '}'
    print(position[j])

for j in range(len(b)**2):
    # HOW TO APPEND A DICTIONARY KEY?
    if j - b[j] > -1:
        # add a left move
        # HOW TO APPEND A PAIR KEY-VAL?  
    elif j + b[j] < len(b):

    elif j -

1 Answers

0
fberg On

To add keys and values to a dictionary, simply write: my_dictionary[key] = value, after initializing your dictionary as my_dictionary = dict(). For your other question i think a bit more information is needed.