2014-10-16 64 views
-1

输入示例:如何在文件解压缩嵌套循环:Python的

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) 
+0

这段代码甚至执行了吗?这些行: 'inLoop = True' 'loopLines.append(extract(line))'它们在'if'语句和'elif'之间,这是无效的 – smac89 2014-10-16 22:10:32

+0

@ smac89。我纠正了它。应该在elif内...缩进错误.. – Doodle 2014-10-16 22:17:13

+1

欢迎使用stackoverflow。如果您更多地了解您的问题而不是更多地了解您的解决方案,您将在这里获得更好的答案如果我不清楚你想要解决什么问题,我可能不会试着回答。一些提示,以改善您的问题:什么是所需的输出/行为? '{LineX}'事情真的是你的输入文件的一部分? – 2014-10-16 22:48:00

回答

0

确定好,如果输入的文件是不是你的,那么你不能做与可怕的格式。我认为处理嵌套最简单的方法是使用递归函数。

约怪异的输入格式一些假设:

  • 的“环行”永远启动的“循环”的下一行。
  • 我忽略了标签。对于“跳”,只是意味着“结束循环”

反正输出我得到的都是符合您的预期,但如果我的假设是不正确的,你需要对此进行修改。

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) 

编辑 - >下面是跳转到标签,而不是修改后的版本。适用于当前的示例输入,但有很多方法可能会导致输入错误。

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) 
+0

我同意输入文件格式是愚蠢的岩石!谢谢。你告诉我的第一个条件有时候不是真的。 “循环”之后和“标签”之前有几行。 – Doodle 2014-10-17 23:02:53

+0

@ FredS我们是否需要检查标签,如果在Jump语句中没有包含循环旁边的一行? – Doodle 2014-10-18 03:28:47

+0

@ Fred S分号不是拼写错误。我修改了这个问题。另外,在分号后的每个标签语句中都有一个“空白”,所以我需要修改它。否则它会在标签声明中断。 – Doodle 2014-10-20 23:10:40