当然可以,以下是两道关于Python NumPy的考试题:

  1. 将一个列表 [1, 2, 3, 4, 5] 转换为NumPy数组,并将其中的奇数元素替换为0。
import numpy as np

lst = [1, 2, 3, 4, 5]
arr = np.array(lst)
arr[arr%2!=0] = 0
print(arr)

输出:[0 2 0 4 0]

  1. 创建一个3x3的NumPy数组,其中每个元素的值等于其行列索引的乘积。
import numpy as np

arr = np.zeros((3,3))
for i in range(3):
    for j in range(3):
        arr[i,j] = i*j
print(arr)

输出:

[[0. 0. 0.]
 [0. 1. 2.]
 [0. 2. 4.]]
可以出两道关于python numpy的考试题吗

原文地址: https://www.cveoy.top/t/topic/fFdq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录