Complete the code to save two arrays into a single .npz file using np.savez.
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) np.savez('data.npz', [1]=arr1, arr2=arr2)
You must provide a name for the first array when saving with np.savez. Here, arr1 is the correct name matching the variable.
Complete the code to load the arrays saved in 'data.npz' and print the first array.
import numpy as np loaded = np.load('data.npz') print(loaded[[1]])
When loading arrays from a .npz file, you access them by the names used when saving. Here, the first array was saved as arr1.
Fix the error in the code to save three arrays named 'a', 'b', and 'c' into 'arrays.npz'.
import numpy as np a = np.array([1, 2]) b = np.array([3, 4]) c = np.array([5, 6]) np.savez('arrays.npz', a, [1], c)
When saving multiple arrays without naming them, you must pass the variables in order. Here, b is the variable to include as the second array.
Fill both blanks to create a dictionary of array lengths for words longer than 3 characters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3 using >.
Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.
data = {'a': 1, 'b': -2, 'c': 3}
result = [1]: [2] for k, v in data.items() if v [3] 0}The dictionary comprehension creates keys as uppercase letters using k.upper(), keeps values as v, and filters for values greater than 0 using >.