Skip to content

Commit 3bebec3

Browse files
authored
Update array-iteration.md
1 parent 75423bc commit 3bebec3

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

contrib/numpy/array-iteration.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Understanding these methods is crucial for performing operations on array elemen
77

88
- Iterating using basic `for` loop.
99

10-
**Single-dimensional array iteration**:
10+
### Single-dimensional array
1111

1212
Iterating over a single-dimensional array is straightforward using a basic `for` loop
1313

@@ -18,11 +18,18 @@ arr = np.array([1, 2, 3, 4, 5])
1818
for i in arr:
1919
print(i)
2020
```
21-
**Output** :
21+
22+
#### Output
23+
2224
```python
23-
[ 1 2 3 4 5 ]
25+
1
26+
2
27+
3
28+
4
29+
5
2430
```
25-
**Multi-dimensional array**:
31+
32+
### Multi-dimensional array
2633

2734
Iterating over multi-dimensional arrays, each iteration returns a sub-array along the first axis.
2835

@@ -32,14 +39,16 @@ marr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
3239
for arr in marr:
3340
print(arr)
3441
```
35-
**Output** :
42+
43+
#### Output
44+
3645
```python
3746
[1 2 3]
3847
[4 5 6]
3948
[7 8 9]
4049
```
4150

42-
## 2. Iterating with nditer
51+
## 2. Iterating with `nditer`
4352

4453
- `nditer` is a powerful iterator provided by NumPy for iterating over multi-dimensional arrays.
4554
- In each interation it gives each element.
@@ -51,7 +60,9 @@ arr = np.array([[1, 2, 3], [4, 5, 6]])
5160
for i in np.nditer(arr):
5261
print(i)
5362
```
54-
**Output** :
63+
64+
#### Output
65+
5566
```python
5667
1
5768
2
@@ -61,7 +72,7 @@ for i in np.nditer(arr):
6172
6
6273
```
6374

64-
## 3. Iterating with ndenumerate
75+
## 3. Iterating with `ndenumerate`
6576

6677
- `ndenumerate` allows you to iterate with both the index and the value of each element.
6778
- It gives index and value as output in each iteration
@@ -74,7 +85,7 @@ for index,value in np.ndenumerate(arr):
7485
print(index,value)
7586
```
7687

77-
**Output** :
88+
#### Output
7889

7990
```python
8091
(0, 0) 1
@@ -86,7 +97,6 @@ for index,value in np.ndenumerate(arr):
8697
## 4. Iterating with flat
8798

8899
- The `flat` attribute returns a 1-D iterator over the array.
89-
-
90100

91101
```python
92102
import numpy as np
@@ -96,7 +106,7 @@ for element in arr.flat:
96106
print(element)
97107
```
98108

99-
**Output** :
109+
#### Output
100110

101111
```python
102112
1
@@ -105,5 +115,6 @@ for element in arr.flat:
105115
4
106116
```
107117

108-
Understanding the various ways to iterate over NumPy arrays can significantly enhance your data processing efficiency.
118+
Understanding the various ways to iterate over NumPy arrays can significantly enhance your data processing efficiency.
119+
109120
Whether you are working with single-dimensional or multi-dimensional arrays, NumPy provides versatile tools to iterate and manipulate array elements effectively.

0 commit comments

Comments
 (0)