arange()与range()的区别

  • 基本理解

  • range(start, end, step),返回一个list对象也就是range.object,起始值为start,终止值为end,但不含终止值,步长为step。只能创建int型list。

  • arange(start, end, step),与range()类似,也不含终止值。但是返回一个array对象。需要导入numpy模块(import numpy as np或者from numpy import*),并且arange可以使用float型数据。
  • 实例

  • >>> from numpy import*
    >>> arange(1,1.9,0.1)  #可以是float型
    array([ 1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8])
    >>> range(1,10,2)         #range(1,10,2)不会生成[1,3,5,6,9]而是生成一个list对象
    >>> for value in range(1,10,2):
     print(value)
  •  
    1
    3
    5
    7
    9
    >>> valuelist=list(range(1,10,1))
    >>> print(valuelist)
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> valuelist=list(range(1,10,0.1))      #range必须是int型
    >>> print(valuelist)
    SyntaxError: multiple statements found while compiling a single statement
    >>>
     

猜你喜欢

转载自blog.csdn.net/qq_39355550/article/details/81779374