使用 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.