Python学习之基本数据类型 range

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
翻译:range类型代表着一段不可变的数字序列而且它经常被用作for里面来指定循环次数。

class  range ( stop )
class  range ( startstop [step ] )

The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
翻译:range构造方法的参数必须都是整数类型(不论是int或者是其他继承实现了指定方法__index__的类型)。如果参数step没有被指定,那么它默认为1.如果其实的参数没有被指定,那么默认的值为0,如果step被指定为0,那么就会抛出ValueError的异常。

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i>= 0 and r[i] < stop.
翻译:如果step是正整数,那么对于r序列来说r[i] = start + step*i where i>= 0 and r[i] < stop. 也就是说step限定了迭代的 

For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
翻译:如果step是负数,同样这个区间的范围由公式r[i] = start + step*i决定,但是限定条件却是 i >= 0 and r[i] > stop.

A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative , but these are interpreted as indexing from the end of the sequence determined by the positive indices.
翻译:如果没有满足r[0]条件的数字,那么将会是一个空的区间。range支持负数,但是索引的含义是由后往前n个,而且n个是由这个序列的正索引的数目决定的。(即数组的大小)

Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raise OverflowError.
翻译:range可以容纳的容量比sys.maxsize所允许的范围要打,但是有些特性可能会抛出OverflowError。比如(len())

maxsize -- the largest supported length of containers.

Range examples:

>>>
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]

Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern).
翻译:range继承实现了序列的所有接口除了拼接和重复意外(因为range对象严格的遵循一个有规律序列的原则。重复和拼接将会破坏这种特性)

start

The value of the start parameter (or 0 if the parameter was not supplied)
翻译:起始下标,如果没有给定默认从0开始

stop

The value of the stop parameter
翻译:终止下标。

step

The value of the step parameter (or 1 if the parameter was not supplied)
翻译:每一次的步长(如果没有给定默认为1,类比于 i++)

The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the startstop and step values, calculating individual items and subranges as needed).
翻译:使用range对list和tuple进行访问的优势就是:range类型只会占用以下部分的内存,不管range所代表的区间有多大(因为它只存储了start,stop,step这三个值,然后进行独立的计算,用一次计算一次,也可以进行裁剪子序列)

Range objects implement the collections.abc.Sequence ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices (see Sequence Types — list, tuple, range):
翻译:range类型继承实现了collections.abc.Sequence ABC,  而且包含了以下几种特性:包含关系测试,元素索引,截取并且支持负索引(参见相关文档)

>>>
>>> r = range(0, 20, 2)
>>> r
range(0, 20, 2)
>>> 11 in r
False
>>> 10 in r
True
>>> r.index(10)
5
>>> r[5]
10
>>> r[:5]
range(0, 10, 2)
>>> r[-1]
18

Testing range objects for equality with == and != compares them as sequences. That is, two range objects are considered equal if they represent the same sequence of values. (Note that two range objects that compare equal might have different startstop and step attributes, for example range(0) == range(2, 1, 3) or range(0,3, 2) == range(0, 4, 2).)
翻译:测试range对象是否相等可以通过 == 或者 !=来对它们进行比较。如果两个序列所代表的集合所有元素值都是一样的,那么一般会被认为是相等的。(tips:两个range相等,有可能start,stop,step不一样,但是所表达的范围都是一样的。比如:range(0) == range(2, 1, 3) or range(0,3, 2) == range(0, 4, 2)

Changed in version 3.2: Implement the Sequence ABC. Support slicing and negative indices. Test int objects for membership in constant time instead of iterating through all items.
翻译:在版本3.2中有变化,支持了截取和负索引。对于int类型的索引是固定时间而不是遍历所有的元素。

Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range objects based on the sequence of values they define (instead of comparing based on object identity).
翻译:在3.3版本。range之间使用==或!=是比较这两个定义的值而不是比较两个是否为同一个对象。

New in version 3.3: The startstop and step attributes.

See also

  • The linspace recipe shows how to implement a lazy version of range that suitable for floating point applications.
    翻译: linspace recipe 将会演示怎样去继承一个lazy版本的range,(lazy版本适合浮点数)




猜你喜欢

转载自blog.csdn.net/rubikchen/article/details/80694380