Skip to content

Commit b0a1091

Browse files
committed
More on Numpy array indexing
1 parent f611215 commit b0a1091

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

09 Numeric Arrays and Data Visualization/1 NumPy - numeric arrays and calculations.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,32 @@ print(A > 2)
248248
# [[False False True]
249249
# [ True True True]]
250250

251-
print(A[A > 2])
252-
# [3 4 5 6]
251+
print(A[A > 2])Holter
253252

254253
print(A[A % 2 == 0])
255254
[2 4 6]
256255
```
257256

257+
### Assigning new values to arrays
258+
259+
You can use any of the above indexing method to set the values of the array, provided you assign a constant or an array with the right shape.
260+
261+
Examples:
262+
263+
```python
264+
A = np.array([[1,2,3], [4,1,1], [0,8,9]])
265+
print(A)
266+
267+
A[1, 1:] = [5, 0]
268+
print(A)
269+
270+
A[A == 0] = 6, 7
271+
print(A)
272+
273+
A[A < 5] = 0
274+
print(A)
275+
```
276+
258277
More: <http://docs.scipy.org/doc/numpy/user/basics.indexing.html>
259278

260279

0 commit comments

Comments
 (0)