简体   繁体   English

在两个特定行之间提取时

[英]DropWhile to extract between two specific lines

practising using DropWhile in Python, and have hit a bump. 练习在Python中使用DropWhile,并且遇到了麻烦。

For example if this is line in a file: 例如,如果这是文件中的行:

Test1
Test2
Test3
Test4
Test5
Test6
Test7
Test8
Test9
Test10

And I want to pull out the lines between Test5 and Test8. 我想拉出Test5和Test8之间的界线。

I know how to do this another way (for line in file...get the last number of line...if line > 5....if line < 8...print); 我知道如何做另一种方式(对于文件中的行...获取行的最后编号...如果行> 5 ....如果行<8 ...打印); but I specifically want to practise using DropWhile. 但我特别想练习使用DropWhile。

I tried this a few different ways but I can't seem to get it to work: 我尝试了几种不同的方法,但似乎无法正常工作:

eg 例如

dataset = open(sys.argv[1]).readlines()
def print_out(line):
    if int(line.strip()[-1]) > 5:
        if int(line.strip()[-1]) < 8:
            return True
        else:
            return False

for line in dropwhile(lambda line: print_out(line) == True, dataset):
    print line.strip()

This doesn't work, all lines are printed out. 这行不通,所有行都已打印出来。

Another way I tried to use a long lambda expression in the dropwhile line instead of using a separate function, but when I did something like this: 我尝试在dropwhile行中使用长的lambda表达式而不是使用单独的函数,但是当我这样做时:

for line in dropwhile(lambda line: 5 < int(line.strip()[-1]) < 8, dataset):

This code works if I only have one expression (ie int(line.strip()[-1]) > 5 or int(line.strip()[-1]) < 8, but not both). 如果我只有一个表达式(即int(line.strip()[-1])> 5或int(line.strip()[-1])<8,但不是全部),则此代码有效。

I'm wondering if someone could show me a pythonic way, using DropWhile, to pull out the lines between Test5 and Test8 in my test data set? 我想知道是否有人可以使用DropWhile向我展示pythonic方式,以拔出测试数据集中Test5和Test8之间的界线?

DropWhile is not what you need, from a functional aproach you have to use filter: DropWhile不是您所需要的,从功能上来说,您必须使用filter:

filter(lambda line: 5 < int(line.strip()[-1]) < 8, dataset)

DropWhile will stop once the condition is reached one time, so the list will continue to have the rest of the values once it reach Test6 DropWhile将在条件一度达到时停止,因此一旦达到Test6 ,列表将继续具有其余值

If you plan to use dropwhile() on your dataset, then you need to also make use of takewhile() to grab the required lines as follows: 如果您打算在数据集上使用dropwhile() ,那么还需要利用takewhile()来抓取所需的行,如下所示:

from itertools import takewhile, dropwhile


for line in takewhile(lambda x:  int(x.strip()[-1]) < 8, dropwhile(lambda x:  int(x.strip()[-1]) <= 5, dataset)):
    print line.strip()

This would give you: 这将为您提供:

Test6
Test7

So it works in two steps, first dropping each line until the required start point, and then only taking lines until the required end point, at which point it completes. 因此,它分两步进行,首先放下每行直到所需的起点,然后才抽出一行直到所需的终点,直到完成为止。

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

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