Python 3 范围与 Python 2 范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44571718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:09:41  来源:igfitidea点击:

Python 3 range Vs Python 2 range

pythonlistrange

提问by Hari Narayana Batta

I recently started learning python 3.
In python 2range()function can be used to assign list elements.

我最近开始学习python 3。
python 2range()中可以使用函数来分配列表元素。

>>> A = []
>>> A = range(0,6)
>>> print A
[0, 1, 2, 3, 4, 5]

where as in python 3when range()function is used this is happening

python 3 中,当使用range()函数时,正在发生这种情况

>>> A = []
>>> A = range(0,6)
>>> print(A)
range(0, 6)

why is this happening?
why did python do this change?
Is it a boon or a bane ?

为什么会这样?
为什么python做了这个改变?
这是福还是祸?

回答by Sam Hartman

Python 3uses iteratorsfor a lot of things where python 2used lists.The docsgive a detailed explanation including the change to range.

Python 3使用迭代器来处理Python 2使用列表的很多事情。文档给出了详细的解释,包括对range.

The advantage is that Python 3doesn't need to allocate the memory if you're using a large range iterator or mapping. For example

优点是如果您使用大范围迭代器或映射,Python 3不需要分配内存。例如

for i in range(1000000000): print(i)

requires a lot less memory in python 3. If you do happen to want Python to expand out the list all at once you can

在 python 3 中需要更少的内存。如果你碰巧希望 Python 一次展开列表,你可以

list_of_range = list(range(10))

回答by Leonard2

in python 2, rangeis a built-in function. below is from the official python docs. it returns a list.

在python 2中,range是一个内置函数。以下来自官方python文档。它返回一个列表。

range(stop)
range(start, stop[, step])
This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops.

range(stop)
range(start, stop[, step])
这是一个多功能函数,用于创建包含等差数列的列表。它最常用于 for 循环。

also you may check xrangeonly existing in python 2. it returns xrangeobject, mainly for fast iteration.

你也可以检查xrange只存在于python 2中。它返回xrange对象,主要用于快速迭代。

xrange(stop)
xrange(start, stop[, step])
This function is very similar to range(), but returns an xrange object instead of a list.

xrange(stop)
xrange(start, stop[, step])
这个函数与 range() 非常相似,但是返回一个 xrange 对象而不是一个列表。

by the way, python 3 merges these two into one rangedata type, working in a similar way of xrangein python 2. check the docs.

顺便说一句,python 3 将这两种range数据类型合并为一种数据类型,以与xrangepython 2 中类似的方式工作。检查文档

回答by Emmanuel Mtali

Python 3 range()function is equivalent to python 2 xrange()function not range()

Python 3range()函数等价于 python 2xrange()函数不range()

Explanation

解释

In python 3 most function return Iterable objects not lists as in python 2 in order to save memory. Some of those are zip()filter()map()including .keys .values .items()dictionary methods But iterable objects are not efficient if your trying to iterate several times so you can still use list()method to convert them to lists

在 python 3 中,大多数函数返回 Iterable 对象而不是像在 python 2 中那样列出以节省内存。其中一些zip()filter()map()包括 . keys .values .items()字典方法但是如果您尝试迭代多次,可迭代对象效率不高,因此您仍然可以使用list()方法将它们转换为列表

回答by Stack

In python3, do

在python3中,做

A = range(0,6)
A = list(A)
print(A)

You will get the same result.

你会得到同样的结果。