range xrange iterator generator

range creates a list, so if you do range(1, 10000000) it creates a list in memory with 10000000elements.

xrange is a generator, so it is a sequence object is a that evaluates lazily.


所以 xrange 可以优化内存的使用

For performance, especially when you're iterating over a large range, xrange() is usually better. However, there are still a few cases why you might prefer range():

  • In python 3, range() does what xrange used to do and xrange does not exist. If you want to write code that will run on both Python 2 and Python 3, you can't use xrange.

  • range() can actually be faster in some cases - eg. if iterating over the same sequence multiple times. xrange has to reconstruct the integer object every time, but range will have real integer objects. (It will always perform worse in terms of memory however)

  • xrange isn't usable in all cases where a real list is needed. For instance, it doesn't support slices, or any list methods.

iterator is a more general concept: any object whose class has a next method (__next__ in Python 3) and an __iter__ method that does return self.

Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions (yield statements, in Python 2.5 and earlier), and is an object that meets the previous paragraph's definition of an iterator.

You may want to use a custom iterator, rather than a generator, when you need a class with somewhat complex state-maintaining behavior, or want to expose other methods besides next (and __iter__and __init__). Most often, a generator (sometimes, for sufficiently simple needs, a generatorexpression) is sufficient, and it's simpler to code because state maintenance (within reasonable limits) is basically "done for you" by the frame getting suspended and resumed.

http://late.am/post/2012/06/18/what-the-heck-is-an-xrange

range() 和 xrange() 是两个用于在 Python 的 for 循环中迭代一定次数的函数。在 Python 2 中,可以使用 range() 和 xrange(),而在 Python 3 中只能使用 range()。 range() 返回一个列表(list)对象,而 xrange() 返回一个 xrange 对象。因此,使用 range() 会占用更多的内存,而使用 xrange() 则占用更少的内存。 在 Python 2 中,xrange() 与 range() 的使用方式相同,但返回的是一个 xrange 对象。而在 Python 3 中,range() 的行为类似于 Python 2 中的 xrange()。因此,如果要编写在 Python 2 和 Python 3 上都可以运行的代码,应该使用 range()。 xrange() 在生成很大的数字序列时比 range() 的性能更好,因为它不需要一开始就分配一个大块的内存空间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Python 中的 range() 与 xrange()】](https://blog.csdn.net/m0_66106755/article/details/127794705)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [range和xrange的区别详解](https://blog.csdn.net/weixin_30783629/article/details/99066868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值