Complete the code to save the array 'data' to a text file named 'output.txt'.
import numpy as np data = np.array([1, 2, 3, 4, 5]) np.[1]('output.txt', data)
The np.savetxt() function saves an array to a text file. Here, it saves 'data' to 'output.txt'.
Complete the code to load data from the text file 'output.txt' into a numpy array.
import numpy as np loaded_data = np.[1]('output.txt') print(loaded_data)
The np.loadtxt() function reads data from a text file and returns it as a numpy array.
Fix the error in the code to save a 2D array 'arr' to 'matrix.txt' with comma separators.
import numpy as np arr = np.array([[1, 2], [3, 4]]) np.savetxt('matrix.txt', arr, delimiter=[1])
The delimiter parameter specifies the character separating values. For CSV style, use a comma ',' as delimiter.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['apple', 'bat', 'car', 'door'] 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 len(word) > 3 filters words longer than 3 letters.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 3.
words = ['apple', 'bat', 'car', 'door'] lengths = { [1]: [2] for word in words if len(word) [3] 3}
word.lower() instead of uppercase.The dictionary comprehension maps each word in uppercase (word.upper()) to its length (len(word)) only if the length is greater than 3 (>).