检查矩阵键是否处于运行状态

2024-09-28 05:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个矩阵文件,并且第一列用作python字典的键(称为docid),我应该如何读取该文件,以便在该键未按运行顺序运行时停止,即

  • if docid-1 > previous_docid
  • if docid < previd

我一直按照下面的代码来做,但看起来有点冗长,有没有其他方法可以产生相同的输出(注意:解决方案需要处理高达20GB的矩阵文件。为了获得代码片段,我给出了一个小数据集)

text = '''0 1 1
0 2 1
1 3 1
1 7 1
2 5 4
2 4 6
2 9 8
3 5 7
3 9 8
3 10 9
9 2 9
9 8 3
3 9 4'''

from collections import defaultdict
docs = defaultdict(list)
previd = -1
for line in text.split('\n'):
    docid, termid, val = map(int,line.split())
    if docid < previd or docid-1 > previd:
        print line
        break
    previd = docid
    docs[docid].append((termid,val))

for i in docs:
    print i, docs[i]

Tags: 文件代码textindocsforifline
1条回答
网友
1楼 · 发布于 2024-09-28 05:20:38

我看不到任何简化,因为过滤条件取决于前面的元素(使潜在的过滤迭代变得复杂)。我认为您的代码并不复杂,但您可以定义一个特殊的遍历:

def read_text(text):
    for line in text.split('\n'):
        docid, termid, val = map(int,line.split())
        if docid < previd or docid-1 > previd:
            print line # I guess this is a debug feature
            return # or raise Exception("line not in running order", line)
        yield (docid, termid, val)

在主代码中:

for docid, termid, val in read_text(text):
    docs[docid].append((termid,val))

编辑:

也许text.split('\n')open('myfile','r')更有效

for line in open('myfile','r'):
    do_something(line)

相关问题 更多 >

    热门问题