Are two arrays element-wise equal?

Are two arrays element-wise equal?

EasyNumPy Programs · 8 of 100

Write a NumPy program to test whether two arrays are element-wise equal.

Input
[3, 8, 5], [3, 8, 5]
Output
[ True  True  True] True

Approach

  1. == compares element by element; np.array_equal gives one answer.

Solution

Python
import numpy as npa, b = np.array([3, 8, 5]), np.array([3, 8, 5])print(a == b, np.array_equal(a, b))
Output
[ True  True  True] True

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Is a value a scalar?

Is a value a scalar?

EasyNumPy Programs · 7 of 100

Write a NumPy program to test whether a value is a scalar.

Input
8.9, [8.9]
Output
True False

Approach

  1. np.isscalar().

Solution

Python
import numpy as npprint(np.isscalar(8.9), np.isscalar([8.9]))
Output
True False

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise real test

Element-wise real test

EasyNumPy Programs · 6 of 100

Write a NumPy program to test element-wise for real numbers.

Input
[2, 4, 6+9j, 7]
Output
[ True  True False  True]

Approach

  1. np.isreal().

Solution

Python
import numpy as npprint(np.isreal(np.array([2, 4, 6 + 9j, 7])))
Output
[ True  True False  True]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise complex test

Element-wise complex test

EasyNumPy Programs · 5 of 100

Write a NumPy program to test element-wise for complex numbers.

Input
[2, 4, 6+9j, 7]
Output
[False False  True False]

Approach

  1. np.iscomplex() is True where the imaginary part is non-zero.

Solution

Python
import numpy as npprint(np.iscomplex(np.array([2, 4, 6 + 9j, 7])))
Output
[False False  True False]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise NaN test

Element-wise NaN test

EasyNumPy Programs · 4 of 100

Write a NumPy program to test element-wise for NaN.

Input
[2, 4, np.nan, 7]
Output
[False False  True False] [False False False False]

Approach

  1. np.isnan(); x == np.nan never works because NaN is not equal to itself.

Solution

Python
import numpy as npa = np.array([2, 4, np.nan, 7])print(np.isnan(a), a == np.nan)
Output
[False False  True False] [False False False False]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise finite test

Element-wise finite test

EasyNumPy Programs · 3 of 100

Write a NumPy program to test element-wise whether values are finite.

Input
[6, 8, 3, np.inf]
Output
[ True  True  True False]

Approach

  1. np.isfinite().

Solution

Python
import numpy as npprint(np.isfinite(np.array([6, 8, 3, np.inf])))
Output
[ True  True  True False]
Note

The original sample output was inverted; isfinite is True for normal numbers and False for infinity.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Test that no element is zero

Test that no element is zero

EasyNumPy Programs · 2 of 100

Write a NumPy program to test whether none of the elements of an array is zero.

Input
[6, 8, 3, 5]
Output
True False

Approach

  1. np.all(arr) is True when every element is non-zero.

Solution

Python
import numpy as npprint(np.all(np.array([6, 8, 3, 5])), np.all(np.array([6, 0, 3])))
Output
True False

Line by line

Line 2

Zero counts as False, so a single 0 makes all False.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Get the NumPy version

Get the NumPy version

EasyNumPy Programs · 1 of 100

Write a Python program to get the installed NumPy version.

Output
2.5.4

Approach

  1. np.__version__.

Solution

Python
import numpy as npprint(np.__version__)
Output
2.5.4

Line by line

Line 1

np is the standard alias for NumPy.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Sum of unique values across lists

Sum of unique values across lists

EasyDictionary Programs · 94 of 94

Write a Python program to add up the distinct numbers found in all list values.

Input
{"a": [6, 7, 2, 8, 1], "b": [2, 3, 1, 6, 8, 10], "d": [1, 8, 2, 6, 9]}
Output
[1, 2, 3, 6, 7, 8, 9, 10] 46

Approach

  1. Union of all values in a set, then sum.

Solution

Python
d = {"a": [6, 7, 2, 8, 1], "b": [2, 3, 1, 6, 8, 10], "d": [1, 8, 2, 6, 9]}unique = set().union(*d.values())print(sorted(unique), sum(unique))
Output
[1, 2, 3, 6, 7, 8, 9, 10] 46

Line by line

Line 2

set().union(*lists) merges all the lists into one set of distinct numbers.

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Reverse values and decorate keys

Reverse values and decorate keys

EasyDictionary Programs · 93 of 94

Write a Python program to reverse each string value and put underscores around each key.

Input
{"a": "Python", "b": "Programming", "c": "Learning"}
Output
{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}

Approach

  1. Dict comprehension with an f-string key and [::-1] value.

Solution

Python
d = {"a": "Python", "b": "Programming", "c": "Learning"}print({f"_{k}_": v[::-1] for k, v in d.items()})
Output
{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs