Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在文件中嵌套循环行n次(示例3)如下:Python_Python_Nested Loops - Fatal编程技术网

在文件中嵌套循环行n次(示例3)如下:Python

在文件中嵌套循环行n次(示例3)如下:Python,python,nested-loops,Python,Nested Loops,我有一个文件,其中的行需要重复的次数与第一行中提到的十进制/十六进制值相同 输入示例: Loop 3 {Line1} ##the line is within curly braces so I used regex but not printing it out right. Label: Blank {Line2} Jump Label {Line3} Line1 Line2 Line3 Line2 Line3 Line2 Line3 输出示例: Loop 3 {Line1

我有一个文件,其中的行需要重复的次数与第一行中提到的十进制/十六进制值相同

输入示例:

Loop  3  {Line1}  ##the line is within curly braces so I used regex but not printing it out right. 
Label: Blank {Line2}
Jump Label {Line3} 
Line1
Line2
Line3
Line2
Line3
Line2
Line3
输出示例:

Loop  3  {Line1}  ##the line is within curly braces so I used regex but not printing it out right. 
Label: Blank {Line2}
Jump Label {Line3} 
Line1
Line2
Line3
Line2
Line3
Line2
Line3
到目前为止,我的代码是:

line_1_ = re.compile(r'Loop\s*([0-9]+|0x[0-9a-fA-F]+)\s*\{(\w+)\}', re.DOTALL)
for match in line_1_.finditer(inputfileContents):
    looptimes =  int(match.group(1))
    line_1 = match.group(2)
    jump = re.search(r'Jump\s*(\w+)\s*\{(.*?)\}', inputfileContents, re.DOTALL)
    label = re.search(r'(\w+):\s*(\w+)\s*\{(\w+)\}', inputfileContents, re.DOTALL)
    for i in range(looptimes):
        if jump.group(1) == label.group(1):
            print '%s\n%s' % (label.group(3), jump.group(2))
错误:我不能用line++增加一行。我理解它是一个字符串,但不确定如何增加它

实际上,它只是重复第2行和第3行3次。但如果跳转和循环之间有多行,则必须打印从Label语句开始到跳转语句(包括跳转语句)的所有行

如果在jump和label语句之间有多行,这将不起作用

例2:

Blank {Line0}
Loop  3  {Line1}  
Label: Blank {Line2}
       Blank {Line3}
       Blank {Line4}
Jump Label {Line5}
Blank {Line6}
上述情况下的预期输出:

Line0
Line1
Line2
Line3
Line4
Line5
Line2
Line3
Line4
Line5
Line2
Line3
Line4
Line5
Line6
例3:

Blank  {Line0}
Loop 3 {Line1}
Label2 {Line2}
Blank  {Line3}
Loop 2 {Line4}  
Label1:{Line5}
Blank  {Line6}
Jump Label1 {Line7}
Blank  {Line8}
Jump Label2 {Line9}
Blank  {Line10}
我需要的输出:

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

我试图从这里实现@Samy Arous方法:但到目前为止无法实现

我发现没有正则表达式更容易:

import sys

infile = sys.stdin

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

for first in infile:
    if first.startswith('Loop '):
        break
    else:
        print extract(first)

count = int(first.split()[1])
lines = [extract(first)] + map(extract, infile)

while count > 0:
    print '\n'.join(lines)
    count -= 1

当一个文件中有多个循环语句时,这种方法有效

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
    loopLines.append(extract(line))
    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)

欢迎来到堆栈溢出!看起来你想让我们为你写些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是包括您迄今为止编写的代码、示例输入(如果有)、预期输出以及您实际获得的控制台输出、回溯等。。你提供的细节越多,你可能得到的答案就越多。检查and。我可能不应该使用正则表达式,但使用ragex,我可以检查行的每个部分。。我不能更早地发布它,因为我的代码没有朝正确的方向发展……你的输出示例是你想要的,还是你得到的?我现在就得到了输出。但我需要更改它,以便它能够打印跳转和标签之间的多行。。现在,如果我在两个语句之间添加几行代码,我的代码将中断。我已经编辑了一些可能的输入,但我的代码不适用于这些输入。我使用正则表达式,因为这些不是填充中的唯一行,它有很多,我正在匹配那些整型的行。好的,你可以通过分割行,检查第一个标记,如果它是循环、标签或跳转等来完成这部分。。我认为你仍然不需要正则表达式。无论如何,到目前为止,您发布的所有示例输入都与我上面编写的代码一起工作;也许您可以添加一个更完整的示例,其中包含预期的输出。谢谢,是的,它可以工作。你能为你使用的返回声明提供一个链接吗?我试图理解这一点..我只是返回表达式行[start:end]旁边的一段行,其中start是要包含的第一个字符,即{的前面一个字符,end是要排除的第一个字符,即最后一个}。我只是添加了一行,这会打断它。我的文件很大,我有许多不同种类的行。。因此,在我读取时处理每一行,当我读取循环行时,我需要多次循环跳转语句