本文介绍了枚举溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

python2.4中的
,我从一个文件中读取行,其中包含


for lineNum,行枚举(f) ):...


但是,lineNum很快就会溢出并开始向后计数。如何使用
i force枚举返回长整数?


干杯。

Hello all,

in python2.4, i read lines from a file with

for lineNum, line in enumerate(f): ...

However, lineNum soon overflows and starts counting backwards. How do
i force enumerate to return long integer?

Cheers.

推荐答案



最有可能你不能,因为它是我认为的C语言函数。


但是作为python 2.4有生成器,自己创建枚举很容易:

def lenumerate(f):

i = 0

for line in f:

收益我,行

i + = 1


Diez

Most probably you can''t, because it is a C-written function I presume.

But as python 2.4 has generators, it''s ease to create an enumerate yourself:
def lenumerate(f):
i = 0
for line in f:
yield i, line
i += 1

Diez




如何很快你究竟从文件中读取了sys.maxint行吗?我b / b
应该认为需要花费大量时间才能获得
读取2,147,483,647行...


但是它确实,Python 2.5使用enumobject表示,

将索引限制为(C)long:


typedef struct {

PyObject_HEAD

long en_index; / *当前枚举索引* /

PyObject * en_sit; / *枚举的辅助迭代器* /

PyObject * en_result; / *结果元组* /

} enumobject;


问候

Steve

-

Steve Holden +1 571 484 6266 +1 800 494 3119

Holden Web LLC / Ltd

Skype:holdenweb


对不起,狗吃了我的.sigline

Just how "soon" exactly do you read sys.maxint lines from a file? I
should have thought that it would take a significant amount of time to
read 2,147,483,647 lines ...

But it is true that Python 2.5 uses an enumobject representation that
limits the index to a (C) long:

typedef struct {
PyObject_HEAD
long en_index; /* current index of enumeration */
PyObject* en_sit; /* secondary iterator of enumeration */
PyObject* en_result; /* result tuple */
} enumobject;

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline




如何很快你究竟从文件中读取了sys.maxint行吗?我b $ b应该认为需要花费大量时间才能获得
读取2,147,483,647行...

Just how "soon" exactly do you read sys.maxint lines from a file? I
should have thought that it would take a significant amount of time to
read 2,147,483,647 lines ...



适度(但不是压倒性地)很长时间:


(定义我们自己的xrange-ish生成器,它可以处理比
大于longs的东西)

A modestly (but not overwhelmingly) long time:

(defining our own xrange-ish generator that can handle things
larger than longs)



.... i = 0

....而i< x:

....收益我

.... i + = 1

....

.... i = 0
.... while i < x:
.... yield i
.... i += 1
....



....

Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

断言错误

我花了大约60-90分钟来打断一个断言

双核2.8ghz机器在轻载下。如果

批量处理冗长的日志文件或其他大数据如

基因数据,则完全有可能达到此限制为OP

被发现。


-tkc

....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError
It took me about an 60-90 minutes to hit the assertion on a
dual-core 2.8ghz machine under otherwise-light-load. If
batch-processing lengthy log files or other large data such as
genetic data, it''s entirely possible to hit this limit as the OP
discovered.

-tkc


这篇关于枚举溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:49