在python3中
print(range(10))range(0,10)
得出的结果是 range(0,10) ,而不是[0,1,2,3,4,5,6,7,8,9] ,为什么呢?
而且原来Python2版本中的xrange也已经取消。
官网原话:
In many ways the object returned by range() behaves as if it is
 a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.
We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the for statement
 is such an iterator. The function list() is another; it creates
 lists from iterables:
翻译:
可以看到上面这个很奇怪,在很多种情况下,range()函数返回的对象的行为都很像一个列表,但是它确实不是一个列表,它只是在迭代的情况下返回指定索引的值,但是它并不会在内存中真正产生一个列表对象,这样也是为了节约内存空间。
我们称这种对象是可迭代的,或者是可迭代对象,还有一种对象叫迭代器,它们需要从一个可迭代对象中连续获取指定索引的值,一直到索引结束。list()函数就是这样一个迭代器,它可以把range()函数返回的对象变成一个列表。
总结:
range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。
list() 函数是对象迭代器,把对象转为一个列表。返回的变量类型为列表。
————————————————
版权声明:本文为CSDN博主「marsjhao」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/marsjhao/article/details/56853316

最新文章

  1. React Native 之生命周期
  2. logsatsh input 插件之 collectd
  3. restful架构的理解
  4. android 语言切换过程分析
  5. 对JAVA集合进行遍历删除时务必要用迭代器
  6. linux 关机方式
  7. PS微观效果
  8. LeetCode_Roman to Integer
  9. 框架基础:ajax设计方案(三)---集成ajax上传技术
  10. JavaScript ES6中export及export default的区别
  11. vue简单的自由拖拽
  12. SpringCloud系列——Ribbon 负载均衡
  13. 关于JPasswordField的getText()方法过时问题解决
  14. Python-接口自动化(七)
  15. flume初识
  16. 转:ActiveMQ的作用总结(应用场景及优势)
  17. mysql 时间戳转换 、cnd、dns 通俗理解
  18. [转帖]QEMU和QEMU-KVM的关系
  19. windows 下运行 UglifyJS
  20. ACM中的取模

热门文章

  1. Windows Phone 页面之间参数传递方法
  2. 【C++基础教程】第四课
  3. 手机UI自动化之显示点触位置(触摸轨迹)
  4. umi request 请求资源库详解
  5. 免费iApp后台-云接口
  6. 使用metaweblog API实现通用博客发布 之 API测试
  7. Jenkins持续集成体系 | 最完整的介绍及资料
  8. js 命令模式 组合模式
  9. 鸿蒙内核源码分析(进程通讯篇) | 九种进程间通讯方式速揽 | 百篇博客分析OpenHarmony源码 | v28.03
  10. P5180-[模板]支配树