简体   繁体   English

Python:选择满足条件的索引范围

[英]Python: select range of indices satisfying a condition

Example input file: 输入文件示例:

index   score
1       0.2
3       0.4
7       1.5
13      1.8
22      1.9
25      1.6
30      0.5
31      0.2
43      1.5
45      1.6
50      0.3
61      0.1

What I want to do is produce something where the min and max indices of the regions that have a score >= 1 print out on the same line like this: 我想做的是生成这样的东西,其中分数> = 1的区域的最小和最大索引在同一行上打印出来,如下所示:

min     max
7       25
43      45

I was trying to use a for loop to split the input lines inside a while loop that checks if the second element is >= 1, but I'm getting myself confused. 我试图使用for循环在while循环内拆分输入行,该循环检查第二个元素是否> = 1,但是我感到困惑。 Any help greatly appreciated. 任何帮助,不胜感激。 Thank you! 谢谢!

Here is my attempt but it's silly because it calls the split line before it splits it: 这是我的尝试,但这很愚蠢,因为它在拆分之前先调用拆分线:

while lp[1] >= 1:  
     for line in file:  
     lp = line.split()  
           if lp[1] >= 1:  
           print(lp[0])  

An easier way to do this is to use itertools.groupby : 一个更简单的方法是使用itertools.groupby

from itertools import groupby
with open('abc1') as f:
    for k, g in groupby(f, key=lambda x:float(x.split()[1]) >= 1):
        if k:
            minn = next(g)
            for maxx in g: pass
            print minn, maxx
...             
7       1.5
25      1.6

43      1.5
45      1.6

Of course the above code will not work in case the range is of length 1, that I'll leave for you to solve. 当然,如果范围的长度为1,则上面的代码将不起作用,我将留给您解决。

lines = []
with open("this_file.txt", "r") as f:
    for l in f.readlines()[1:]:
        a, b = l.split()
        lines.append( (int(a), float(b) )
bigger_than_one = [ pair for pair in lines if pair[1]>=1. ]
print("min", *min(bigger_than_one, key= lambda x: x[1]))
print("max", *max(bigger_than_one, key= lambda x: x[1]))

Untested, but you should get an idea. 未经测试,但是您应该有一个主意。

Thank you both, I learned something from looking into your answers. 谢谢你们,我从调查您的答案中学到了一些东西。 I ended up writing this: 我最终写了这个:

previousScore = 0
previousID = ""
offpeak = True

for line in file:
        lp = line.split()
        if float(lp[1]) >= 1 and offpeak:
                print(lp[0]) 
                offpeak = False
        if float(lp[1]) <= 1 and previousScore >=1:
                print(previousID) 
                offpeak = True
        previousScore = float(lp[1])
        previousID = lp[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM