如何在給定的範圍內建立一個 numpy 陣列?


我們必須在使用者提供的範圍內建立一個 numpy 陣列。我們將使用 numpy 庫中的 arange() 函式來獲得我們的輸出。

演算法

Step1: Import numpy.
Step 2: Take start_value, end_value and Step from the user.
Step 3: Print the array using arange() function in numpy.

示例程式碼

import numpy as np

start_val = int(input("Enter starting value: "))
end_val = int(input("Enter ending value: "))
Step_val = int(input("Enter Step value: "))
print(np.arange(start_val, end_val, Step_val))

輸出

Enter starting value: 5
Enter ending value: 50
Enter Step value: 5
[ 5 10 15 20 25 30 35 40 45]

更新於:16-Mar-2021

1K+ 瀏覽

開啟你的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.