Compare arrays
Out of the box, you can also use comparison operators with NumPy arrays.
Remember areas, the list of area measurements for different rooms in your house from Introduction to Python? This time there's two NumPy arrays: my_house and your_house. They both contain the areas for the kitchen, living room, bedroom and bathroom in the same order, so you can compare them.
Diese Übung ist Teil des Kurses
Intermediate Python
Anleitung zur Übung
Using comparison operators, generate boolean arrays that answer the following questions:
- Which areas in
my_houseare greater than or equal to18? - You can also compare two NumPy arrays element-wise. Which areas in
my_houseare smaller than the ones inyour_house? - Make sure to wrap both commands in a
print()statement so that you can inspect the output!
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Create arrays
import numpy as np
my_house = np.array([18.0, 20.0, 10.75, 9.50])
your_house = np.array([14.0, 24.0, 14.25, 9.0])
# my_house greater than or equal to 18
# my_house less than your_house