Find a specific element in a list with sub-lists (Python)

0

I'm fine-tuning a Pathfindig code based on A *,

I need to check and modify an element within a list with several sub-lists, but I have doubts if I use a loop repetition, or if there is already some function that does this, (That returns as value the position of this element in the list ).

V = 3, 3 # V equivale ao elemento[0] de cada sub-lista.
lista = [[[1,3] 2,1 , 40, 90], [[2,3] 3,2 , 60, 50], [[3,3] 4,2 , 70, 15]...]

How can I for example check if V is in the list and if so, how can I change it. Thank you!

    
asked by anonymous 11.04.2018 / 02:18

1 answer

2

If I understand what you want to do, then it might be best for you to use a dictionary with position tuples as keys:

>>> posicoes = {(1, 3): [2, 1, 40, 90], (2, 3): [3, 2, 60, 50]}
>>> posicoes[3, 3] = [4, 2, 70, 15]
>>> print(posicoes)
{(1, 3): [2, 1, 40, 90], (2, 3): [3, 2, 60, 50], (3, 3): [4, 2, 70, 15]}
>>> (3, 3) in posicoes
True
>>> posicoes[3, 3]
[4, 2, 70, 15]
>>> posicoes[3, 3][2] = 80
>>> posicoes[3, 3]
[4, 2, 80, 15]
    
11.04.2018 / 02:43