好吧我承认最近一直入门python! 关于python3 和 python2 List 的一点区别 以及python 通过enumerate 内建函数遍历List
2016-05-06 19:59
921 查看
来我们直接上代码
再看这个
很有趣不是么?
我们都知道python遍历数组是十分方便的。
但是如何在遍历数组的同时获得当前下标?for i in range(len(b)): ?再通过下标寻址?
所幸python提供了一个极为方便的内建函数enumerate()
可以看到成功的输出了当前下标!
同时我们也可以从任意位置开始
Python 2.7.9 (default, Mar 1 2015, 12:57:24) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> x = 'momoda' >>> dy = [x for x in 'NICE'] >>> x 'E' >>>
再看这个
Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = 'momoda' >>> dy = [x for x in 'NICE'] >>> x 'momoda'
很有趣不是么?
我们都知道python遍历数组是十分方便的。
>>> b = [i for i in range(10)] >>> b [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for i in b: ... print(i) ... 0 1 2 3 4 5 6 7 8 9
但是如何在遍历数组的同时获得当前下标?for i in range(len(b)): ?再通过下标寻址?
所幸python提供了一个极为方便的内建函数enumerate()
>>> for no, i in enumerate(b): ... print(no, i) ... 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
可以看到成功的输出了当前下标!
同时我们也可以从任意位置开始
>>> for no, i in enumerate(b): ... print(no, i) ... 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法