Complete the code to create a contiguous NumPy array from a Python list.
import numpy as np arr = np.array([1, 2, 3, 4, 5], [1]='C') print(arr.flags['C_CONTIGUOUS'])
The order parameter controls the memory layout. Setting order='C' creates a contiguous array in row-major order.
Complete the code to check if a NumPy array is stored contiguously in memory.
import numpy as np arr = np.arange(10) is_contiguous = arr.flags.[1] print(is_contiguous)
The C_CONTIGUOUS flag indicates if the array is stored in contiguous C-style (row-major) memory layout.
Fix the error in the code to make the array contiguous after slicing.
import numpy as np arr = np.arange(10) sliced = arr[::2] contiguous = sliced.[1]() print(contiguous.flags['C_CONTIGUOUS'])
Slicing with steps creates a non-contiguous view. Using copy() creates a new contiguous array.
Fill both blanks to create a 2D contiguous array and check its memory layout.
import numpy as np arr = np.array([[1, 2], [3, 4]], [1]='[2]') print(arr.flags['C_CONTIGUOUS'])
Setting order='C' creates a contiguous array in row-major order.
Fill all three blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.
words = ['data', 'science', 'ai', 'ml'] lengths = { [1]: [2] for word in words if [3] }
The dictionary comprehension uses the word as key, its length as value, and filters words with length greater than 3.