Python数据分析(中英对照)·Ranges 范围

作者 : jamin 本文共2328个字,预计阅读时间需要6分钟 发布时间: 2020-10-18 共1105人阅读

1.2.4: Ranges 范围

范围是不可变的整数序列,通常用于for循环。
Ranges are immutable sequences of integers,and they are commonly used in for loops.
要创建一个范围对象,我们键入“range”,然后输入范围的停止值。
To create a range object, we type "range" and then we put in the stopping value of the range.
现在,我们刚刚创建了一个范围对象,但是如果您想查看该对象的实际内容,那么这就没有多大帮助了。
Now, we’ve just created a range object, but this is less helpful if you would like to see what’s the actual content of that object.
虽然,我们通常不会在Python程序中这样做,但为了真正看到该范围对象的内容,我们可以在这种情况下将其转换为列表。
Although, we wouldn’t typically do this in a Python program,for us to really see the content of that range object,so what we can do in this case is we can turn it into a list.
所以如果我们说“范围5列表”,我们会看到范围对象由五个数字组成,从0到4。
So if we say "list of range 5," we’ll see that the range object consists of five numbers, from 0 to 4.
范围的输入参数是停止值。
The input argument to range is the stopping value.
记住,Python在到达停止值之前停止。
And remember, Python stops before it hits the stopping value.
这就是为什么范围5实际上不包含数字5。
That’s why range 5 does actually not contain the number 5.
我们可以为range函数提供额外的参数。
We can provide additional arguments to the range function.
例如,我们可以提供起点,也可以定义步长。
For example, we can provide the starting point,and we can also define the step size.
所以如果我们输入“range1到6”,在这种情况下,我们得到一个range对象,它从1开始,到5结束。
So if we type "range 1 to 6," in that case,we get a range object which starts at 1 and ends at 5.
如果我们想以2为增量,我们可以这样做。
If we wanted to go in increments of two, we could do something like this.
我们可以从1开始,一直到13——13号,不包括它本身——我们可以分两步走。
We could start from 1, go up to 13– number 13,not itself included– and we could go in steps of two.
在本例中,我们得到一个从1开始到11结束的范围对象。
In this case, we get a range object that starts at 1 and ends at 11.
通常,当我们在Python程序中使用范围对象时,我们不会首先将它们转换为列表。
Typically when we use range objects in our Python programs,we do not first turn them into lists.
我们在这里这样做只是为了让我们更容易理解这些对象的作用。
We’ve done it here only so that it’s easier for us to understand what these objects do.
当然,您可以在for循环上下文中使用list对象,但由于以下原因,它是有问题的。
You can certainly use a list object in a for loop context,but it’s problematic for the following reason.
为了存储范围对象,Python只存储三个不同的数字,即起始数字、停止数字及其步长。
To store a range object, Python is only storing three different numbers,which are the starting number, the stopping number, and its step size.
如果您有一个非常大的数据集,其中包含(比如)1000万个对象,如果您首先创建一个包含用于访问的索引的列表
If you have a very large dataset that contains, say, 10 million objects,if you first create a list that contains the indices for accessing
这1000万个数字,只是为了能够循环浏览数据而浪费了很多空间。
these 10 million numbers, you’ve just wasted a lot of space just to be able to loop through your data.
因此,请按原样使用范围对象。
Consequently, use range objects as is.
在使用它们之前,不要将它们变成列表。
Don’t turn them into lists before using them.

本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络 » Python数据分析(中英对照)·Ranges 范围

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用

提供最优质的资源集合

立即加入 友好社区
×