Python使用精确位置计算文件中的行数

Python使用精确位置计算文件中的行数,python,list,for-loop,count,Python,List,For Loop,Count,我知道这很简单,但我不太明白如何使for循环工作 我的第一个文件是两列数据的长列表: ROW VALUE 0 165 1 115 2 32 3 14 4 9 5 0 6 89 7 26 . . 406369 129 406370 103 我的第二个文件是重要行号的列表: 1 43 192 so on 我要做的就是转到文件1中感兴趣的行号,然后逐行向下走,直到值列达到零。然后,输出将只是

我知道这很简单,但我不太明白如何使for循环工作

我的第一个文件是两列数据的长列表:

ROW    VALUE
0      165   
1      115
2      32
3      14
4      9
5      0
6      89
7      26
.       .
406369  129
406370  103
我的第二个文件是重要行号的列表:

1
43
192
so on
我要做的就是转到文件1中感兴趣的行号,然后逐行向下走,直到值列达到零。然后,输出将只是一个重要行号列表,后跟行数,直到第一个文件达到零为止。例如,文件#2中重要行号“1”的输出应该是3,因为有三行,然后文件#1中的值达到0。谢谢你的帮助!我有一些脚本,我已经开始,可以张贴在编辑它,如果这是有益的。谢谢大家!

编辑:

我已经开始编写一些脚本:

for line in important_rows_file:
    line = line.strip().split()
    positive_starts.append(int(line[2])

countsfile = []
for line in file:
    line = line.strip().split()
    countsfile.append([line[0]] + [line[1]])

count = 0
i = 0
for i in range(0, len(countsfile)):
    for start in positive_starts:
    if int(countsfile[start + i][1]) > 0:
            count = count + 1
    else:
            count = count

。。。。不确定下一步是什么

这里有两种方法

第一种方法是在内存中为所有行号建立一个字典。如果a。您将反复使用相同的数据(您可以将其存储并读回)或b。您将处理第二个文件中的许多行(即,大多数行都需要这样做)。第二种方法只对给定的行号执行一次性操作

将其作为输入文件:

ROW    VALUE
0      165
1      115
2      32
3      14
4      9
5      0
6      89
7      26
8      13
9      0
方法1

ref_dict = {}
with open("so_cnt_file.txt") as infile:
    next(infile)
    cur_start_row = 0
    cur_rows = []
    for line in infile:
        row, col = [int(val) for val in line.strip().split(" ") if val]
        if col == 0:
            for cur_row in cur_rows:
                ref_dict[cur_row] = row - cur_row - 1
            cur_start_row = row
            cur_rows = []
            continue
        cur_rows.append(row)
print ref_dict
输出

{0: 4, 1: 3, 2: 2, 3: 1, 4: 0, 6: 2, 7: 1, 8: 0}
3
2
[(1, 3), (6, 2)]
方法2

def get_count_for_row(row=1):
    with open("so_cnt_file.txt") as infile:
        for i in range(0, row + 2):
            next(infile)
        cnt = 0
        for line in infile:
            row, col = [int(val) for val in line.strip().split(" ") if val]
            if col == 0:
                return cnt
            cnt += 1
print get_count_for_row(1)
print get_count_for_row(6)
输出

{0: 4, 1: 3, 2: 2, 3: 1, 4: 0, 6: 2, 7: 1, 8: 0}
3
2
[(1, 3), (6, 2)]
下面是一个解决方案,它在一次调用中获取所有感兴趣的行

def get_count_for_rows(*rows):
    rows = sorted(rows)
    counts = []
    with open("so_cnt_file.txt") as infile:
        cur_row = 0
        for i in range(cur_row, 2):
             next(infile)
        while rows:
            inrow = rows.pop(0)
            for i in range(cur_row, inrow):
                next(infile)
            cnt = 0
            for line in infile:
                row, col = [int(val) for val in line.strip().split(" ") if val]
                if col == 0:
                    counts.append((inrow, cnt))
                    break
                cnt += 1
            cur_row = row
    return counts

print get_count_for_rows(1, 6)
输出

{0: 4, 1: 3, 2: 2, 3: 1, 4: 0, 6: 2, 7: 1, 8: 0}
3
2
[(1, 3), (6, 2)]

在您的情况下,行
4
是否应该产生
1
“我有一些脚本,如果有帮助,可以在编辑中发布。”是的,请这样做。同意@MikeSherrill'Catcall'。您应该始终发布您尝试过的内容。第4行应生成0,因为在值列@sberryThank中,第4行和下一个0之间没有行!我将在一个有很多行的文件上使用它,是否有一种方法可以只打印每一行,而不必像在第(1)行的
print get\u count\u中那样调用每一行?