如何解压缩文件中的嵌套循环:Python

时间:2014-10-16 22:04:13

标签: python nested-loops

输入示例:

Blank  {Line0}
Loop 3 {Line1}
Blank  {Line1a}
Label2: Blank {Line2}
Blank  {Line3}
Loop 2 {Line4} 
Blank  {Line4a}      
Label1: Blank {Line5}       # I need to repeat the Jump statements as many times as mentioned in the Loop before the Label statement. Always The Loop statements precedes the immediate Jump statement. So here Line5, Lin6 and Line7 gets repeated twice. Jump to label1 so print line5, Line6 (since it has nothign going on) and Line7 is the end of Jump statement. Repeat it twice. Since Loop 2 {Line4} says do the following Jump Twice. 
Blank  {Line6}
Jump Label1 {Line7}
Blank  {Line8}
Jump Label2 {Line9} # This jumps back to Label2. Which repeats everything until this line including the expanded inner loop ... This is the nested loop.
Blank  {Line10}

输出示例:

Line0
Line1
Line1a
Line2
Line3
Line4
Line4a
Line5
Line6
Line7
Line5
Line6
Line7
Line8
Line9
Line2
Line3
Line4
Line4a
Line5
Line6
Line7
Line5
Line6
Line7
Line8
Line9
Line2
Line3
Line4
Line4a
Line5
Line6
Line7
Line5
Line6
Line7
Line8
Line9
Line10

我目前有一个代码适用于文件中的循环和重复循环。但是上面给出的输入文件中断了。我试图从这里实现@Samy Arous方法:Using Python to Parse a File for Nested Loops但是无法实现它。为清楚起见,将此视为单独的问题。

所有的线条都只是字符串...这是一个有点复杂的格式,所以只给出了一个简单的格式。我们可以假设Blank,JUMP,LOOP都指导着需要做的事情。只需将其打印出来或根据循环和跳转重复

我的代码:

import sys

inputtfile = sys.stdin
inputfileContents = open(r'path to input file')


def extract(line):
    return line[line.find('{') + 1: line.rfind('}')]

loopCount = 0
loopLines = []
inLoop = False
for line in inputfileContents:
    if line.startswith('Loop '):
        loopCount = int(line.split()[1])
        inLoop = True
    elif line.startswith('Jump '):
        loopLines.append(extract(line))
        for i in range(loopCount):
            for loopLine in loopLines:
                print loopLine

#reset state variables to track next possible loop
        loopCount = 0
        inLoop = False
        loopLines = []
    elif inLoop:
        loopLines.append(extract(line))
    else:
        print extract(line)

1 个答案:

答案 0 :(得分:0)

好吧,如果输入文件不是你的,那么你就不能用糟糕的格式做很多事情。我认为处理嵌套最简单的方法是使用递归函数。

关于奇怪的输入格式的几个假设:

  • "循环线"总是在#34; Loop"。
  • 之后的下一行开始
  • 我忽略了标签。对于" jump",这只意味着"结束循环"

无论如何,我得到的输出似乎符合您的预期,但如果我的假设不正确,您需要修改它。

with open(r'path to input file') as f:
    inputfileContents = f.readlines()

def printLines(lines):
    nest_level = 0
    for i, line in enumerate(lines):
        print line.split('{')[-1].split('}')[0]
        if line.startswith('Jump '):
            nest_level -= 1
            if nest_level < 0:
                return
        elif line.startswith('Loop '):
            nest_level += 1
            loopCount = int(line.split()[1])-1
            for cnt in range(loopCount):
                printLines(lines[i+1:])

printLines(inputfileContents)

编辑 - &gt;这是一个修改后的版本,它跳转到标签。使用当前的示例输入,但有很多方法可能会导致错误输入。

with open(r'path to input file') as f:
    inputfileContents = f.readlines()

def printLines(lines):
    loop_cnt = []
    label_to_idx = {}
    for i, line in enumerate(lines):
        line = line.split('#')[0] #removing comments
        print line.split('{')[-1].split('}')[0]
        line_label = line.split('{')[0].strip()
        line_label = line_label.replace(':','') #I assume the colon is a typo?
        label_to_idx[line_label] = i

        if line.startswith('Jump '):
            if not loop_cnt:
                return
            jump_label = line.split()[1]
            start = label_to_idx[jump_label]
            loopCount = loop_cnt.pop()
            for cnt in range(loopCount):
                printLines(lines[start:])
        elif line.startswith('Loop '):
            loopCount = int(line.split()[1])-1
            loop_cnt.append(loopCount)

printLines(inputfileContents)