Negative Indexing
We discussed positive indexing, but there is also negative indexing. Negative indexing starts from the end, with index -1 referring to the last element, index -2 referring to the second-to-last element, and so on.
12345import numpy as np arr = np.array([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) print(arr[-1 , -1])
This example illustrates how to retrieve a value of 10 from a given two-dimensional array using negative indexing.
The first index determines the row we choose (-1 refers to the last row), while the second index corresponds to the element we select within that row (-1 is the last one). As a result, we obtain a value of 10. Run the code above to verify it.
Swipe to start coding
You have the following array:
[[-4, 3, 1], [2, 10, -4]]. Access the value 10.
Let's give it a try. Use only negative indices.
Solução
Obrigado pelo seu feedback!
single
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Resumir este capítulo
Explicar o código em file
Explicar por que file não resolve a tarefa
Awesome!
Completion rate improved to 4.76
Negative Indexing
Deslize para mostrar o menu
We discussed positive indexing, but there is also negative indexing. Negative indexing starts from the end, with index -1 referring to the last element, index -2 referring to the second-to-last element, and so on.
12345import numpy as np arr = np.array([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]) print(arr[-1 , -1])
This example illustrates how to retrieve a value of 10 from a given two-dimensional array using negative indexing.
The first index determines the row we choose (-1 refers to the last row), while the second index corresponds to the element we select within that row (-1 is the last one). As a result, we obtain a value of 10. Run the code above to verify it.
Swipe to start coding
You have the following array:
[[-4, 3, 1], [2, 10, -4]]. Access the value 10.
Let's give it a try. Use only negative indices.
Solução
Obrigado pelo seu feedback!
single