Dictionary for objects in Python

2

I would like to know how I can assign a dictionary to a set of objects.

For example, I have a 4x4 vector of 'neuron' objects: [[<__main__.Neuronio object at 0x1035644e0>, <__main__.Neuronio object at 0x103564518>, <__main__.Neuronio object at 0x103564550>, <__main__.Neuronio object at 0x103564588>], [<__main__.Neuronio object at 0x1035645c0>, <__main__.Neuronio object at 0x1035645f8>, <__main__.Neuronio object at 0x103564630>, <__main__.Neuronio object at 0x103564668>], [<__main__.Neuronio object at 0x1035646a0>, <__main__.Neuronio object at 0x1035646d8>, <__main__.Neuronio object at 0x103564710>, <__main__.Neuronio object at 0x103564748>], [<__main__.Neuronio object at 0x103564780>, <__main__.Neuronio object at 0x1035647b8>, <__main__.Neuronio object at 0x1035647f0>, <__main__.Neuronio object at 0x103564828>]]

And I would like to work with the respective dictionary as if working with the dictionary ('1') would have referenced the map [0] [0], main object. Neuronio object at 0x1035644e0 > and so on consecutively to dictionary (2 ... 3 ... n) referencing the objects of the array.

    
asked by anonymous 01.06.2018 / 23:18

2 answers

3

I was able to do the following:

# criando dicionário para o mapa 
dicionario_mapa = {}

# adicionando um contador para facilitar
contador = 1

# fazer um for percorrer o vetor
for i in range(len(mapa)):
    for j in range(len(mapa[0])):
        # adicionar um novo valor funciona da forma: dicionario[chave] = valor do conteúdo
        dicionario_mapa[contador] = mapa[i][j]
        contador += 1

# mostrar mapa no console
print(dicionario_mapa)

And in response: {1: <__main__.Neuronio object at 0x1046294a8>, 2: <__main__.Neuronio object at 0x1046294e0>, 3: <__main__.Neuronio object at 0x104629518>, 4: <__main__.Neuronio object at 0x104629550>, 5: <__main__.Neuronio object at 0x104629588>, 6: <__main__.Neuronio object at 0x1046295c0>, 7: <__main__.Neuronio object at 0x1046295f8>, 8: <__main__.Neuronio object at 0x104629630>, 9: <__main__.Neuronio object at 0x104629668>, 10: <__main__.Neuronio object at 0x1046296a0>, 11: <__main__.Neuronio object at 0x1046296d8>, 12: <__main__.Neuronio object at 0x104629710>, 13: <__main__.Neuronio object at 0x104629748>, 14: <__main__.Neuronio object at 0x104629780>, 15: <__main__.Neuronio object at 0x1046297b8>, 16: <__main__.Neuronio object at 0x1046297f0>}

    
01.06.2018 / 23:34
2

I start by saying that for elemento in len(range(iteravel)) is always the wrong way to iterate in python. The correct one is to use for elemento in iteravel , or in cases that need the index you can use the function enumerate :

for indice, elemento in enumerate(iteravel):

But in your case, you do not need the index because you are only assigning based on contador . So just do it like this:

dicionario_mapa = {}
contador = 1

for linha in mapa:
    for valor in linha:
        dicionario_mapa[contador] = valor
        contador += 1

print(dicionario_mapa)

See this example working on Ideone

Another interesting solution would be to use chain of itertools , which allows join multiple iterables into one, like already getting a single list instead of list lists. Then with the enumerate function you can get the position you want without having to use the variable contador :

from itertools import chain
dicionario_mapa = {}
for posicao, valor in enumerate(chain(*mapa)):
    dicionario_mapa[posicao + 1] = valor

print(dicionario_mapa)

See also this version on Ideone

One sees that you now only have an assignment in for , you can also make it < in> list comprehension , leaving only one line to assign the values:

from itertools import chain
dicionario_mapa = { posicao+1: valor for posicao, valor in enumerate(chain(*mapa)) }

print(dicionario_mapa)

View this latest version on Ideone

    
02.06.2018 / 10:56