python之enumerate
http://eagletff.blog.163.com/blog/static/116350928201266111125832/
一般情况下,如果要对一个列表或者数组既要遍历索引又要遍历元素时,可以用enumerate
比如:
for index,value in enumerate(list):
print index,value
当然也可以
for i in range(0,len(list)):
print i,list[i]
相比较而言,前者更简练
另外一种使用情况,当要计算文件的行数的时候,可以这样写:
lineNum = len(open(fileName,'r').readlines())
不过如果文件巨大,这种方式可能会造成严重问题,比如机器挂掉
这个时候可以使用enumerate,python cookbook里举出了这样的例子
lineNum = -1
for count,line in enumerate(open(fileName,'r')):
print count
lineNum+=1
print lineNum
试过用上面的脚本来统计一个1GB大小的log文件(最后统计出来大约有18000000行),运行脚本的时候,CPU占用率比较高(看来是做计算的比较多),但是脚本用到的内存不多,显示的python占用内存为5M多
不过如果用readlines的方法,内存飙到了200M
将文本文件的全部内容按照分行,作为一个列表读出的5种方法
list_of_all_the_lines = file_object.readlines( )
list_of_all_the_lines = file_object.read( ).splitlines(1)
list_of_all_the_lines = file_object.read().splitlines( )
list_of_all_the_lines = file_object.read( ).split('\n')
list_of_all_the_lines = list(file_object)
python之enumerate的更多相关文章
- python中enumerate()的用法
先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输出, 2.将 list 倒序成 [6, 5, ...
- python 遍历enumerate
在python中enumerate的用法多用于在for循环中得到计数,本文即以实例形式向大家展现python中enumerate的用法.具体如下: enumerate参数为可遍历的变量,如 字符串,列 ...
- python的enumerate函数
python的enumerate函数用于循环索引和元素 例如 foo = 'abc' for i , ch in enumerate(foo): print ch, '(%d)' % i 输出结果: ...
- python中enumerate()函数用法
python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...
- Python学习笔记之Python的enumerate函数
Python 的 enumerate() 函数就像是一个神秘的黑箱,你无法简单地用一句话来概括这个函数的作用与用法. enumerate() 函数属于非常有用的高级用法,而对于这一点,很多初学者甚至中 ...
- Python内建函数enumerate()用法及在for循环应用
Python 内建函数enumerate() 由于这个单纯很长,不容易记住,用法还是比较广泛的,下面讲述Python内建函数enumerate()用法. 1,实例 enumerate(sequence ...
- [python拾遗]enumerate()函数
在python中处理各类序列时,如果我们想显示出这个序列的元素以及它们的下标,可以使用enumerate()函数. enumerate()函数用于遍历用于遍历序列中的元素以及它们的下标,用法如下: 1 ...
- python中enumerate的使用
在python的应用中,当我们使用一个数组或者列表的时候既要遍历索引又要遍历元素的时候通常的做法是这样的: >>> lsi = [1,2,3,4,5,6,7,8,9] >> ...
- python之enumerate枚举 第二篇(六):enumerate枚举
[Python之旅]第二篇(六):enumerate枚举 python enumerate枚举 摘要: 1.普通情况下打印列表中索引号及其对应元素 使用下面的循环: 1 2 3 4 5 6 ...
- python之enumerate()函数的探究
原地址:http://www.newsmth.net/nForum/#!article/Python/95860 最近用到enumerate()函数,于是查了相关的资料. 其中的一 ...
随机推荐
- CF986A Fair
题目描述 Some company is going to hold a fair in Byteland. There are n n n towns in Byteland and m m m t ...
- c语言中malloc函数的使用
传送门:https://www.cnblogs.com/shiweihappy/p/4246372.html c语言中内存的管理:https://www.cnblogs.com/tuhooo/p/72 ...
- $each 遍历json字符串
$.each遍历json对象 查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1",&qu ...
- 8、kvm虚拟机添加硬盘
kvm虚拟机添加硬盘qemu-img创建一块新的硬盘 qemu-img create -f qcow2 /kvm-data/kvm/jumperhost_disk1.qcow2 50G 关闭虚拟机 v ...
- python之Selenium库的使用
一 什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并 ...
- 解决spark-shell输出日志信息过多
import org.apache.log4j.Logger import org.apache.log4j.Level Logger.getLogger("org").setLe ...
- 【ElasticSearch+NetCore 第一篇】在Windows上安装部署ElasticSearch和ElasticSearch-head
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...
- SpringMVC(二)高级应用
一.参数绑定-----集合类型 二.数据回显(例如提交表单失败了,数据没有丢失) 三.上传图片 四.json数据的交互 五.restful 支持 六.拦截器
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- Hie with the Pie(poj3311)
题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...