使用 NumPy 在给定列表中查找数字的倍数
pythonnumpyserver side programmingprogramming更新于 2024/9/22 3:24:17
在此程序中,我们将找到给定数字的倍数所在的索引位置。我们将使用 Numpy 和 Pandas 库来完成此任务。
算法
步骤 1:定义一个 Pandas 系列。 步骤 2:从用户那里输入一个数字 n。 步骤 3:使用 numpy 库中的 argwhere() 函数从系列中查找该数字的倍数。
示例代码
import numpy as np
listnum = np.arange(1,20)
multiples = []
print("NumList:\n",listnum)
n = int(input("Enter the number you want to find multiples of: "))
for num in listnum:
if num % n == 0:
multiples.append(num)
print("Multiples of {} are {}".format(n, multiples))
输出
NumList: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] Enter the number you want to find multiples of: 5 Multiples of 5 are [5, 10, 15]
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

