技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

使用 NumPy 在给定列表中查找数字的倍数

pythonnumpyserver side programmingprogramming

在此程序中,我们将找到给定数字的倍数所在的索引位置。我们将使用 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]

相关文章