简体   繁体   English

Python在嵌套循环内继续,进入正确的嵌套层次

[英]Python Continue inside nested loops, getting to the right level of the nesting

I am working on something that needs to make it's way through several levels of checking if conditions are met before either exiting all together or setting certain variables and then starting the loop over. 我正在做一些事情,需要通过几个层次的检查方式来检查是否满足条件,然后再全部退出或设置某些变量,然后开始循环。 My question revolves around how to jump back to the primary while loop from the internal for loop. 我的问题围绕着如何从内部for循环跳回主while循环。

while True:
    message = stomp.get
    message = simplejson.loads(message.body)

    if message[0]['fieldname1'] == False:
      global ShutdownState
      ShutdownState = True
      break # Should leave the While loop all together

    else:

      for item in message[0]['fieldname2'][0]['fieldname2-1']

        if item['fieldname2-1-1'] == True:
           list1_new[len(list_new):] = [item['fieldname2-1-2']
           list1-state = set(list1) == set(list1_new)

           if list1-state == True:
               continue # should reset the while loop

        else:
           list1 = list1_new # should print the new list1 and then reset the while loop
           print list1
           continue

It's not clear whether the sample code was meant to be representative of your whole loop, or just the beginning of it. 尚不清楚示例代码是要代表整个循环还是只是循环的开始。 If it was the whole thing, then there are lots of ways you can restructure it. 如果这是全部,那么有很多方法可以对其进行重组。 Here's a first stab (note that are some typos in the code (eg, list1-state rather than list1_state , and things like that), so I've had to adjust some things. You'll need to check whether it still matches up with your original code. (For more about this implementation of finding the first element in the list, and some alternatives, have a look at Python: Find in list .) 这是第一个选项(请注意,代码中有一些错别字(例如list1-state而不是list1_state等),因此我不得不进行一些调整。您需要检查它是否仍然匹配(使用您的原始代码。(有关在列表中查找第一个元素的实现的更多信息以及一些替代方法,请查看Python:在列表中查找 )。

while True:
    message = stomp.get
    message = simplejson.loads(message.body)

    # If the message doesn't match this criterion,
    # we need to abort everything.
    if not message[0]['fieldname1']:
        global ShutdownState
        ShutdownState = True
        break

    try:
        # get the first item in message[0]['fieldname2'][0]['fieldname2-1']
        # such item['fieldname2-1-1'] is true.  Whether we
        # find one and do this code, or don't and catch the
        # StopIteration, we wrap back to the while loop.
        item = next(x
                    for x in message[0]['fieldname2'][0]['fieldname2-1']
                    if item['fieldname2-1-1'])
        list1_new[len(list_new),:] = item['fieldname2-1-2']
        list1_state = (set(list1) == set(list1_new))

        if not list1_state:
            list1 = list1_new # should print the new list1 and then reset the while loop
            print list1
    except StopIteration:
        # There was no such item.
        pass

You might also clean this up by making it a do-while loop, but that's a less major factor. 您也可以通过使其成为do-while循环来进行清理,但这不是主要因素。 Based on Emulate a do-while loop in Python? 基于在Python中模拟do-while循环? , you could do something like: ,您可以执行以下操作:

def get_message():
    message = stomp.get
    return simplejson.loads(message.body)

message = get_message()
while message[0]['fieldname1']:
    try:
        # get the first item in message[0]['fieldname2'][0]['fieldname2-1']
        # such item['fieldname2-1-1'] is true.  Whether we
        # find one and do this code, or don't and catch the
        # StopIteration, we wrap back to the while loop.
        item = next(x
                    for x in message[0]['fieldname2'][0]['fieldname2-1']
                    if item['fieldname2-1-1'])
        list1_new[len(list_new),:] = item['fieldname2-1-2']
        list1_state = (set(list1) == set(list1_new))

        if not list1_state:
            list1 = list1_new # should print the new list1 and then reset the while loop
            print list1
    except StopIteration:
        # There was no such item.
        pass
    message = get_message()

global ShutdownState
ShutdownState = True

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

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