Como fazer referência a elementos em um array em Python
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
]
(
"First Element ="
, first_element)
(
"Second Element ="
, second_element)
(
"Third Element ="
, third_element)
(
"Fourth Element ="
, fourth_element)
(
"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
]
(
"First Element ="
, first_element)
(
"Second Element ="
, second_element)
(
"Third Element ="
, third_element)
(
"Fourth Element ="
, fourth_element)
(
"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
]
(
"First Row Second Column ="
, first_row_second_column)
(
"Second Row First Column ="
, second_row_first_column)
(
"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
]
(
"First Row Second Column ="
, first_row_second_column)
(
"Second Row First Column ="
, second_row_first_column)
(
"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
]
(
"First Second Second Value ="
, first_second_second)
(
"Second First Third Value ="
, second_first_third)
(
"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
]
(
"First Second Second Value ="
, first_second_second)
(
"Second First Third Value ="
, second_first_third)
(
"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
]
(
"First Row ="
, first_row)
(
"Second Row ="
, second_row)
(
"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
]
(
"First Row ="
, first_row)
(
"Second Row ="
, second_row)
(
"Third Row ="
, third_row)
Resultado:
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva