“range(0,2)"和“range(0,2)"有什么区别?和“列表(范围(0,2))"? [英] What is the difference between "range(0,2)" and "list(range(0,2))"?

查看:92
本文介绍了“range(0,2)"和“range(0,2)"有什么区别?和“列表(范围(0,2))"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要理解range(0,2)list(range(0,2))的区别,使用python2.7

两者都返回一个列表,那么到底有什么区别?

解决方案

在 Python 3.x 中,

range(0,3) 返回一类不可变的可迭代对象,让您可以迭代它们,它不会生成列表,并且它们不会将范围内的所有元素都存储在内存中,相反,它们动态生成元素(当您迭代它们时),而 list(range(0,3)) 生成一个列表(通过迭代所有元素并在内部附加到列表) .

示例 -

<预><代码>>>>范围(0,3)范围(0, 3)>>>列表(范围(0,3))[0, 1, 2]

理想情况下,如果您只想迭代该范围的值,range(0,3) 将比 (list(range(0,3)) 快> 因为后者有在开始迭代之前生成列表的开销.

在 Python 2.x 中,range(0,3) 生成一个列表,我们还有一个 xrange() 函数,它具有与 类似的行为Python 3.x 中的 range() 函数(xrange 在 Python 3.x 中重命名为 range)

对于 Python 3.5,来自文档 -

<块引用>

Range 对象实现了 collections.abc.Sequence ABC,并提供了诸如包含测试、元素索引查找、切片和对负索引的支持等功能

所以你可以做这样的事情 -

<预><代码>>>>范围(0,10)[5]5>>>范围(0,10)[3:7]范围(3, 7)>>>5 范围内 (6,10)错误的>>>7 范围内 (1,8)真的

并且所有这些都是恒定时间操作,从这个测试中可以看出 -

在 [11]: %timeit a = xrange(0,1000000)[1000]1000000 个循环,最好的 3 个:每个循环 342 ns在 [12]: %timeit a = xrange(0,1000000)[10000]1000000 个循环,最好的 3 个:每个循环 342 ns在 [13]: %timeit a = xrange(0,1000000)[100000]1000000 个循环,最好的 3 个:每个循环 342 ns在 [14]: %timeit a = xrange(0,1000000)[999999]1000000 个循环,最好的 3 个:每个循环 342 ns在 [15] 中:%timeit a = xrange(0,10000000)[9999999]1000000 个循环,3 个最佳:每个循环 339 ns在 [16]: %timeit a = xrange(0,1000000000000)[9999999999]1000000 个循环,最好的 3 个:每个循环 341 ns

Need to understand the difference between range(0,2) and list(range(0,2)), using python2.7

Both return a list so what exactly is the difference?

解决方案

In Python 3.x ,

range(0,3) returns a class of immutable iterable objects that lets you iterate over them, it does not produce lists, and they do not store all the elements in the range in memory, instead they produce the elements on the fly (as you are iterating over them) , whereas list(range(0,3)) produces a list (by iterating over all the elements and appending to the list internally) .

Example -

>>> range(0,3)
range(0, 3)
>>> list(range(0,3))
[0, 1, 2]

Ideally, if you only want to iterate over that range of values , range(0,3) would be faster than (list(range(0,3)) because the latter has the overhead of producing a list before you start iterating over it.

In Python 2.x , range(0,3) produces an list, instead we also had an xrange() function that has similar behavior of range() function from Python 3.x (xrange was renamed to range in Python 3.x)

For Python 3.5, From the documentation -

Range objects implement the collections.abc.Sequence ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices

So you can do things like -

>>> range(0,10)[5]
5
>>> range(0,10)[3:7]
range(3, 7)
>>> 5 in range(6,10)
False
>>> 7 in range(1,8)
True

And all of these are constant time operations , as can be seen from this test -

In [11]: %timeit a = xrange(0,1000000)[1000]
1000000 loops, best of 3: 342 ns per loop

In [12]: %timeit a = xrange(0,1000000)[10000]
1000000 loops, best of 3: 342 ns per loop

In [13]: %timeit a = xrange(0,1000000)[100000]
1000000 loops, best of 3: 342 ns per loop

In [14]: %timeit a = xrange(0,1000000)[999999]
1000000 loops, best of 3: 342 ns per loop

In [15]: %timeit a = xrange(0,10000000)[9999999]
1000000 loops, best of 3: 339 ns per loop

In [16]: %timeit a = xrange(0,1000000000000)[9999999999]
1000000 loops, best of 3: 341 ns per loop

这篇关于“range(0,2)"和“range(0,2)"有什么区别?和“列表(范围(0,2))"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆