为变量分配范围 (Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18177919/
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-19 10:06:00  来源:igfitidea点击:

Assign a range to a variable (Python)

python

提问by Justin

Whenever I try to assign a range to a variable like so:

每当我尝试为变量分配范围时,如下所示:

Var1 = range(10, 50)

Then try to print the variable:

然后尝试打印变量:

Var1 = range(10, 50)
print(Var1)

It simply prints 'range(10, 50)' instead of all the numbers in the list. Why is this?

它只是打印 'range(10, 50)' 而不是列表中的所有数字。为什么是这样?

采纳答案by Justin

Thats because rangereturns a range object in Python 3. Put it in listto make it do what you want:

那是因为range在 Python 3 中返回一个范围对象。把它放进list去让它做你想做的事:

>>> Var1 = range(10, 50)
>>> print(list(Var1))
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,  
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
>>>

回答by Brigand

This is something that was changed in Python 3.0. You could recreate a similar function in Python 2.7 like so,

这在 Python 3.0 中有所改变。你可以像这样在 Python 2.7 中重新创建一个类似的函数,

def range(low, high=None):
  if high == None:
    low, high = 0, low
  i = low
  while i < high:
    yield i
    i += 1

The benefit of this, is that the list doesn't have to be created before its used.

这样做的好处是,在使用之前不必创建列表。

for i in range(1,999999):
  text = raw_input("{}: ".format(i))
  print "You said {}".format(text)

Which works like this:

它的工作原理是这样的:

1: something
You said something
2: something else
You said something else
3: another thing
You said another thing

In Python 3.X, if we never get to the end of our loop (999997 iterations), it will never calculate all of the items. In Python 2.7 it has to build the entire range initially. In some older Python implementations, this was very slow.

在 Python 3.X 中,如果我们永远不会到达循环的结尾(999997 次迭代),它将永远不会计算所有项目。在 Python 2.7 中,它必须首先构建整个范围。在一些较旧的 Python 实现中,这非常慢。

回答by roippi

You should probably understand a bit of what is happening under the hood.

您可能应该了解一些幕后发生的事情。

In python 3, rangereturns a rangeobject, not a list like you may have been expecting. (n.b. python 3's rangeis python 2's xrange)

在 python 3 中,range返回一个range对象,而不是您可能期望的列表。(nb python 3'srange是 python 2's xrange)

#python 2
>>> type(range(5))
<type 'list'>

#python 3
>>> type(range(5))
<class 'range'>

What does that mean? Well, the range object is simply an iterator - you need to force it to iterate to get its contents. That may sound annoying to the neonate, but it turns out to be quite useful.

这意味着什么?嗯,范围对象只是一个迭代器 - 您需要强制它迭代以获取其内容。这对新生儿来说可能听起来很烦人,但事实证明它非常有用。

>>> import sys
>>> sys.getsizeof(range(5))
24
>>> sys.getsizeof(range(1000000))
24

The biggest advantage of doing things this way: constant (tiny) memory footprint. For this reason, manythings in the standard library simply return iterators. The tradeoff is that if you simply want to seethe iterated range, you have to force it to iterate. The most common idiom to do that is list(range(n)). There's also comprehensions, forloops, and more esoteric methods. Iterators are a huge part of learning python so get used to them!

这样做的最大优点:恒定(微小)内存占用。出于这个原因,标准库中的许多东西只是简单地返回迭代器。权衡是,如果您只是想查看迭代范围,则必须强制它进行迭代。最常见的成语是list(range(n)). 还有理解、for循环和更深奥的方法。迭代器是学习 Python 的重要组成部分,所以要习惯它们!