Pré-requisitos: Numpy

Os elementos de uma matriz podem ser referenciados como uma matriz Python regular. Como o python internamente não oferece suporte a arrays, aqui sempre que usamos o termo array, estamos nos referindo à lista de pythons que pode ser usada para construir um array de qualquer dimensão necessária. Além disso, o módulo NumPy do Python também fornece um contêiner denominado 'array' para armazenar coleções de dados. Neste artigo, falaremos sobre como fazer referência a elementos em uma matriz Python, bem como a uma matriz numpy em Python.

  • Para referência de array, apenas o índice do elemento requerido deve ser passado para o nome do array.

Sintaxe:

array_name [índice]
  • Para referenciar usando um array numpy, primeiro um array é criado usando a função array numpy e depois é referenciado como um array regular.

Sintaxe:

np.array ([elementos da matriz])

A implementação usando os dois métodos é fornecida abaixo para vários casos:



Exemplo 1: Referenciando itens em uma matriz 1-D

Exemplo com Python Array

arr = [4, 6, 3, 9, 2] 
first_element = arr[0] 
second_element = arr[1] 
third_element = arr[2] 
fourth_element = arr[3] 
fifth_element = arr[4] 
print("First Element =", first_element) 
print("Second Element =", second_element) 
print("Third Element =", third_element) 
print("Fourth Element =", fourth_element) 
print("Fifth Element =", fifth_element) 

Resultado:

Exemplo com a matriz do módulo numpy do Python

import numpy as np 
arr = np.array([4, 6, 3, 9, 2]) 
first_element = arr[0] 
second_element = arr[1] 
third_element = arr[2] 
fourth_element = arr[3] 
fifth_element = arr[4] 
print("First Element =", first_element) 
print("Second Element =", second_element) 
print("Third Element =", third_element) 
print("Fourth Element =", fourth_element) 
print("Fifth Element =", fifth_element) 

Resultado:



Exemplo 2: Referenciando itens em uma matriz 2-D

 Exemplo com Python Array

arr = [[4, 6, 3], 
       [5, 9, 2], 
       [1, 8, 7]] 
first_row_second_column = arr[0][1] 
second_row_first_column = arr[1][0] 
third_row_third_column = arr[2][2] 
print("First Row Second Column =", first_row_second_column) 
print("Second Row First Column =", second_row_first_column) 
print("Third Row Third Column =", third_row_third_column) 

Resultado:

Exemplo com a matriz do módulo numpy do Python

import numpy as np 
arr = np.array([[4, 6, 3], 
                [5, 9, 2], 
                [1, 8, 7]]) 
first_row_second_column = arr[0][1] 
second_row_first_column = arr[1][0] 
third_row_third_column = arr[2][2] 
print("First Row Second Column =", first_row_second_column) 
print("Second Row First Column =", second_row_first_column) 
print("Third Row Third Column =", third_row_third_column) 

Resultado:

Exemplo 3: Referenciando Itens em um Array 3D

Exemplo com array Python



arr = [[[4, 6, 3], [2, 6, 8], [3, 5, 12]], 
       [[32, 11, 4], [23, 53, 89], [19, 17, 10]], 
       [[14, 22, 52], [56, 43, 99], [20, 37, 32]]] 
first_second_second = arr[0][1][1] 
second_first_third = arr[1][0][2] 
third_third_first = arr[2][2][0] 
print("First Second Second Value =", first_second_second) 
print("Second First Third Value =", second_first_third) 
print("Third Third First Value =", third_third_first) 

Resultado:

Exemplo com a matriz do módulo numpy do Python

import numpy as np 
arr = np.array([[[4, 6, 3], [2, 6, 8], [3, 5, 12]], 
                [[32, 11, 4], [23, 53, 89], [19, 17, 10]], 
                [[14, 22, 52], [56, 43, 99], [20, 37, 32]]]) 
first_second_second = arr[0][1][1] 
second_first_third = arr[1][0][2] 
third_third_first = arr[2][2][0] 
print("First Second Second Value =", first_second_second) 
print("Second First Third Value =", second_first_third) 
print("Third Third First Value =", third_third_first) 

O utput :

Exemplo 4: Referenciando uma linha inteira de uma matriz

Exemplo com array Python

arr = [[4, 6, 3], 
       [5, 9, 2], 
       [1, 8, 7]] 
first_row = arr[0] 
second_row = arr[1] 
third_row = arr[2] 
print("First Row =", first_row) 
print("Second Row =", second_row) 
print("Third Row =", third_row) 

Resultado:

Exemplo com a matriz do módulo numpy do Python

import numpy as np 
arr = np.array([[4, 6, 3], 
                [5, 9, 2], 
                [1, 8, 7]]) 
first_row = arr[0] 
second_row = arr[1] 
third_row = arr[2] 
print("First Row =", first_row) 
print("Second Row =", second_row) 
print("Third Row =", third_row) 

Resultado: