Python在嵌套循环中继续,进入嵌套的正确级别

时间:2015-01-31 05:46:14

标签: python loops

我正在研究一些需要通过几个级别检查条件是否符合条件的方法,然后再退出所有条件或设置某些变量然后开始循环。我的问题围绕如何从内部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

1 个答案:

答案 0 :(得分:2)

不清楚示例代码是代表您的整个循环,还是只是它的开头。如果是整个事情,那么有很多方法可以重组它。这是第一次尝试(注意代码中有一些拼写错误(例如,list1-state而不是list1_state,以及类似的东西),所以我不得不调整一些东西。您需要检查它是否仍与原始代码匹配。(有关查找列表中第一个元素的实现的更多信息,以及一些替代方法,请查看Python: Find in list。)

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

您也可以通过将其作为do-while循环来清理它,但这不是一个主要因素。基于Emulate a do-while loop in Python?,您可以执行以下操作:

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