是否可以重复循环的迭代?

2024-04-26 14:55:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我定义了一个for循环,它扫描由两列组成的文件,当它找到关键字DEFINE_MENU这行的第二列是指屏幕的标题。关键字的下一个实例将为一个单独的屏幕定义一个标题,依此类推到第n个屏幕。目前,该代码能够定义第一个菜单标题。在

当我到达关键字DEFINE_MENU的第二个实例时,是否可以为同一行重复循环,设置title_flag = 0,从而重复自身,捕获第二个菜单标题?在

def getInfo():

    title_flag = 0
    number = 1
    menus = {}
    items = {}
    title = None
    file = open('some_file', 'r')
    for line in file:
        # Test for comments, if they exist pass and move on
        if line[0] == '#':
            continue
        # Hop over blank lines
        if re.search(r'^\s+$', line):
            continue
        # Find the line where the title is defined
        if re.search('DEFINE_MENU', line) and title_flag == 0:
            type, name = line.split()
            title = name
            title_flag = 1
            continue
        # If DEFINE_MENU is found and flag has been raised, this
        # signifies a new menu definition so break.
        if re.search('DEFINE_MENU', line) and title_flag == 1:
            break
        if re.search('PRIV', line):
            (type, name, priv_holder, *description) = line.split()
        else:
            (type, name, *description) = line.split()
        # If flag has been raised, the line must follow the definition
        # of the menu, it must contains info regarding a menu item.
        if title_flag == 1:
            description = ' '.join(description)
            items[str(number)] = name
            number += 1
    file.close()
    menus[title] = items
    return menus

感谢您的意见!在

汤姆

编辑:首先,为造成的混乱道歉。也许我认为这个问题比我想象的要简单,提供的信息也比要求的少。我将更深入地研究,我使用的输入文件的格式如下:

^{pr2}$

在进一步操作DEFINE_MENU调用之间的数据时,我已经升起了标志。我已经编辑了代码以包含我编写的完整方法。在

结果是当前的字典,其中键是菜单标题,值是另一个包含菜单项(标题后面的值)的字典,如下所示:

{'title1': {'1': 'menu_name1', '3': 'var_name1', '2': 'menu_name2', '5': 'command1', '4': 'var_name2'}}

我想要的是一个更大的字典,包含菜单标题作为键,最低级别的字典作为值。我知道这很复杂,如果不清楚,请告诉我是否需要更多信息。在

再次感谢

汤姆


Tags: andthenamere标题searchiftitle
0条回答
网友
1楼 · 发布于 2024-04-26 14:55:21

我不知道你到底在找什么,但你能不能把你的名字储存在一个列表里?在

titles = []

for line in infile: # don't shadow "file" keyword...

    if "DEFINE_MENU" in line:
        titles.append(line.split()[-1]) # append the last item

如果只需要前两个标题,可以添加额外的测试:

^{pr2}$
网友
2楼 · 发布于 2024-04-26 14:55:21

这是否符合你的逻辑?在

menus = []
current_menu = None
for line in file:
    # ...
    # Find the line where the title is defined
    if re.search('DEFINE_MENU', line):
        if not current_menu is None:
            menus.append(current_menu)
        current_menu = Menu()
        type, name = line.split()
        current_menu.setTitle(name)
        continue

Menu将是一个helper类,用于存储属于一个菜单的所有信息。另外,由于您的所有案例似乎都是互斥的,请考虑使用elif而不是{}。在

网友
3楼 · 发布于 2024-04-26 14:55:21

如果我正确地理解了您的问题描述,您希望重复循环body(在某些情况下),而不是“迭代”。如果是这样,一种可行的方法是将循环体变成一个可能的递归函数,如果循环要中断,则返回True,如果要继续,则返回{},以及可能的标题。例如,一个最直接(虽然可能不是最优雅的)函数:

def body(line):
    global title_flag

    # skip over comments
    if line[0] == '#':
        return None, False
    # skip over blank lines
    if re.search(r'^\s+$', line):
        return None, False

    # Find the line where the title is defined
    if 'DEFINE_MENU' in line and title_flag == 0:
        type, name = line.split()
        title = name
        title_flag = 1
        return title, body(line)

    # If DEFINE_MENU is found and flag has been raised, this
    # signifies a new menu definition.
    if 'DEFINE_MENU' in line and title_flag == 1:
        return None, True

主线代码变成:

^{pr2}$

顺便说一句:donot通过使用内置名称来命名自己的变量,比如file这是一个单独的主题;-)。BTW2:我使用with(假设Python2.6或更高版本,或者2.5和“import from the future”)只是因为这是打开和关闭文件的正确方法;-)。BTW3:我删除了re.search,因为当一个简单的in操作符显然做了相同的工作时,它是肆意的,令人费解的过度杀戮。在

我发布这段代码主要是为了以一种具体的方式来检查我是否理解你的规范,因为,从行为角度来说,这绝对是一种过激行为,没有必要(从你发布的代码)重复检查注释和空行,也没有搜索'DEFINE_MENU",等等。。。您可以在设置了标题后立即退出(然后body可能会再次成为内联代码)。在

但是,我想你发帖是有原因的,也就是说,这个简单的行为(而且可以更简单地说)对你来说不是最理想的,也许通过看到你的字面问题的解决方案,你可以指出你想要的不同于此,然后我们可以帮助你。在

相关问题 更多 >