So verweisen Sie auf Elemente in einem Array in Python
Voraussetzungen: Numpy
Elemente eines Arrays können wie ein reguläres Python-Array referenziert werden. Da Python intern keine Arrays unterstützt, beziehen wir uns hier immer dann, wenn wir den Begriff Array verwenden, auf die Python-Liste, mit der ein Array mit einer beliebigen erforderlichen Dimension erstellt werden kann. Darüber hinaus bietet das NumPy- Modul von Python einen Container mit dem Namen 'array' zum Speichern von Datensammlungen. In diesem Artikel wird erläutert, wie Sie Elemente in einem Python-Array sowie ein Numpy- Array in Python referenzieren .
- Bei der Array-Referenzierung muss nur der Index des erforderlichen Elements an den Namen des Arrays übergeben werden.
Syntax:
Array-Name [Index]
- Für die Referenzierung mit dem numpy-Array wird zuerst ein Array mit der Array-Funktion von numpy erstellt und dann wie ein reguläres Array referenziert.
Syntax:
np.array ([Array-Elemente])
Die Implementierung mit beiden Methoden ist unten für verschiedene Fälle angegeben:
Beispiel 1: Referenzieren von Elementen in einem 1-D-Array
Beispiel mit 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)
Ausgabe:
Beispiel mit dem Array des Python-Numpy-Moduls
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)
Ausgabe:
Beispiel 2: Referenzieren von Elementen in einem 2D-Array
Beispiel mit 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)
Ausgabe:
Beispiel mit dem Array des Python-Numpy-Moduls
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)
Ausgabe:
Beispiel 3: Referenzieren von Elementen in einem 3D-Array
Beispiel mit Python-Array
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)
Ausgabe:
Beispiel mit dem Array des Python-Numpy-Moduls
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 :
Beispiel 4: Referenzieren einer gesamten Zeile eines Arrays
Beispiel mit Python-Array
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)
Ausgabe:
Beispiel mit dem Array des Python-Numpy-Moduls
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)
Ausgabe: