Python2升级到Python3的时候,我们会注意到xrange报错
这时建议将xrange换成range
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(range(6))
<type 'list'>
python2中,range的返回值是list,这意味着内存将会分布相应的长度的空间给list。
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(range(6))
<class 'range'>
python3中返回值是一个对象,并没有将数据完全实例化,所以内存中只有一个对象的空间,对性能优化还是很有帮助的。
当然了你也可以在python3写一个xrange
def xrange(x):
n=0
while n<x:
yield n
n+=1
参考:https://blog.csdn.net/mvs2008/article/details/73693012
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/31490526/viewspace-2565083/