当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。