All values except 20
EasyNumPy Programs · 24 of 100
Write a NumPy program to print all values of the vector 1-20 except 20.
Output[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Approach
- Slice off the last element, or filter.
Solution
Python
import numpy as npv = np.arange(1, 21)print(v[:-1])print(v[v != 20])
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Learn the concept
New to this topic? Read the lesson, then come back.
More numpy programs
← All 100 NumPy Programs