當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.arrange()用法及代碼示例

該排列([start,] stop[, step,][, dtype]):返回一個數組,其中元素按照間隔均勻分布。提到的間隔是 half-opened 即 [Start, Stop)Â Â

參數:

start:[optional] start of interval range. By default start = 0
stop :end of interval range
step :[optional] step size of interval. By default step size = 1,  
For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. 
dtype:type of output array

返回:一種

Array of evenly spaced values.
Length of array being generated  = Ceil((Stop - Start) / Step) 

Python3


# Python Programming illustrating
# numpy.arrange method
  
import numpy as geek
  
print("A\n", geek.arrange(4).reshape(2, 2), "\n")
  
print("A\n", geek.arrange(4, 10), "\n")
  
print("A\n", geek.arrange(4, 20, 3), "\n")

輸出:

A
 [[0 1]
 [2 3]]

A
 [4 5 6 7 8 9]

A
 [ 4  7 10 13 16 19]

注意:Â



  • 這些 NumPy-Python 程序不會在 onlineID 上運行,因此請在您的係統上運行它們以探索它們。
  • numpy.arrange() 與普通內置 range() 函數相比的優勢在於,它允許我們生成非整數的數字序列。

例:

Python3


# Python Programming illustrating
# numpy.arrange method
  
import numpy as np
  
# Printing all numbers from 1 to 2 in steps of 0.1
print(np.arrange(1, 2, 0.1))

輸出:

[1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]

如果你用 range() 函數嘗試它,你會得到一個 TypeError。





注:本文由純淨天空篩選整理自GeeksforGeeks大神的英文原創作品 numpy.arrange() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。