博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xrange vs range python
阅读量:4032 次
发布时间:2019-05-24

本文共 4881 字,大约阅读时间需要 16 分钟。

http://pythoncentral.io/how-to-use-pythons-xrange-and-range/

Python has two handy functions for creating lists, or a range of integers that assist in making for loops.

These functions are xrange and range. But you probably already guessed that! :)

The Difference Between xrange and range in Python

Before we get started, let's talk about what makes xrange and range different.

For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and xrange returns an xrange object.

What does that mean? Good question! It means that xrange doesn't actually generate a static list at run-time like range does. It creates the values as you need them with a special technique called yielding. This technique is used with a type of object known as generators. If you want to read more in depth about generators and the yield keyword, be sure to checkout the article .

Okay, now what does that mean? Another good question. That means that if you have a really gigantic range you'd like to generate a list for, say one billion, xrange is the function to use. This is especially true if you have a really memory sensitive system such as a cell phone that you are working with, as range will use as much memory as it can to create your array of integers, which can result in a MemoryError and crash your program. It's a memory hungry beast.

That being said, if you'd like to iterate over the list multiple times, it's probably better to use range. This is because xrange has to generate an integer object every time you access an index, whereas range is a static list and the integers are already "there" to use.

Alright, now on to the good stuff.

How to Use Python's range and xrange

So how do we use range and xrange? Here is the simplest example:

Great! Simple enough. Note that you could also use range in place of xrange here, but I personally like xrange better. Maybe it's that sexy "x" in the front. :)

Alright, explanation time. The functions xrange and range take in three arguments in total, however two of them are optional. The arguments are "start", "stop" and "step". "start" is what integer you'd like to start your list with, "stop" is what integer you'd like your list to stop at, and "step" is what your list elements will increment by.

Python's xrange and range with Odd Numbers

Say we wanted only odd numbers. Here's what we could do:

We told Python that we would like the first element to be one, the last element to be one less than ten, and that we'd like each element to go up by two. Really simple stuff.

Python's xrange and range with Negative Numbers

Alright, so what about if we want a negative list?

Simple. Here's an example:

That's it! All we have to do is change the "start", "stop" and "step" to negative numbers. Awesome. Please note that you must do it this way for negative lists. Trying to use xrange(-10) will not work because xrange and range use a default "step" of one.

Another Python xrange and range Example

Here's one more example of even numbers between 100 and 120:

And that's pretty much it for xrange and range. If you would like a more technical explanation of what these two functions do, please consult the Python docs for  and .

Note that if  "start" is larger than "stop", the list returned will be empty. Also, if "step" is larger that "stop" minus "start", then "stop" will be raised to the value of  "step" and the list will contain "start" as its only element.

Here's a clearer picture:

Deprecation of Python's xrange

One more thing to add. In Python 3.x, the xrange function does not exist anymore. The range function now does what xrange does in Python 2.x, so to keep your code portable, you might want to stick to using range instead. Of course, you could always use the  tool that Python provides in order to convert your code, but that introduces more complexity.

The reason why xrange was removed was because it is basically always better to use it, and the performance effects are negligible. So Python 3.x's range function is xrange from Python 2.x.

You can find the PEP document  for reasoning why it was removed.

Over and out, soldier. Over and out.

转载地址:http://ajhbi.baihongyu.com/

你可能感兴趣的文章
python一句话之利用文件对话框获取文件路径
查看>>
PaperDownloader——文献命名6起来
查看>>
PaperDownloader 1.5.1——更加人性化的文献下载命名解决方案
查看>>
如何将PaperDownloader下载的文献存放到任意位置
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
自己动手写GC
查看>>
Java 8新特性终极指南
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
可扩展、高可用服务网络设计方案
查看>>
如何构建高扩展性网站
查看>>
微服务架构的设计模式
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
c++指针常量与常量指针详解
查看>>
c++字符数组和字符指针区别以及str***函数
查看>>