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

在这个程序中,我们将找到给定数字的倍数存在的索引位置。我们将同时使用 Numpy 和 Pandas 库来完成这项任务。

算法

Step 1: Define a Pandas series.
Step 2: Input a number n from the user.
Step 3: Find the multiples of that number from the series using argwhere() function in the numpy library.

示例代码

import numpy as np

listnum = np.arange(1,20)
multiples = []

print("NumList:\n",listnum)
n = int(input("输入要查找倍数的数字: "))
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]
输入要查找倍数的数字: 5
Multiples of 5 are [5, 10, 15]

猜你喜欢