使用 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("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]

更新日期:16-Mar-2021

2K+ 次浏览

开启您的职业生涯

完成课程,获取认证

开始
广告
© . All rights reserved.