python-3.x 如果满足条件,则从第2次迭代开始跳过

g6baxovj  于 2023-04-08  发布在  Python
关注(0)|答案(2)|浏览(128)
#Can be more than two items also.
item_list = ["rpm", "exe"]

extraction_status = True
while extraction_status:
    file_status_flag = True
    
    file_status = some_other_function(arg1, arg2) # returns a nested dictionary of lists.
    
    for item in file_status['BU']: # file_status['BU'] = ["HR", "DEV" ,"Admin"]
        if item['extensions'] in item_list: #rpm or exe or zip
            if (file_status['status'] == "PUSHED")):
                print("file from {} reached target..".format(item))
                #do further action on that file
                continue # not working
            else:
                print("Status of {} is {}".format(item, file_status['status']))
    # To check if all the items in item_list has been pushed.               
    if (file_status_flag and (file_status['status'] == "PUSHED")):
        "All files reached target..make the script successful.."
        extraction_status = False

让我解释一下,如果HR在第一次或迭代中处于PUSHED状态,则从后续迭代开始执行某些操作,如果满足条件则跳过。
continue跳过了当前的迭代,这不是我想要的。
这可能吗?
示例输出

1st iteration:
file from BU reached target
file from DEV In Progress
file from Admin Not Prepared

2nd iteration: 
file from DEV In Progress
file from Admin In Progress

所以..

q8l4jmvw

q8l4jmvw1#

为什么不添加另一个标志来查看你是否已经检查了条件?例如,我们可以添加一个名为already_checked的标志,在第一次迭代时将其设置为True,然后,如果它已经是True,则跳过进一步的操作:

item_list = ["rpm", "exe"]

extraction_status = True
while extraction_status:
    file_status_flag = True
    already_checked = False    # add this flag 
    
    file_status = some_other_function(arg1, arg2)
    
    for item in file_status['BU']: 
        if (item['extensions'] in item_list) and not already_checked:
            if (file_status['status'] == "PUSHED")):
                print("file from {} reached target..".format(item))
                #do further action on that file
                already_checked = True     # on the first successful iteration set the flag
            else:
                print("Status of {} is {}".format(item, file_status['status']))
    # To check if all the items in item_list has been pushed.               
    if (file_status_flag and (file_status['status'] == "PUSHED")):
        "All files reached target..make the script successful.."
        extraction_status = False
8gsdolmq

8gsdolmq2#

我不知道有什么魔法可以做到这一点。但是你可以使用一些额外的变量:

#Can be more than two items also.
item_list = ["rpm", "exe"]

extraction_status = True
first_iteration = True

while extraction_status:
    file_status_flag = True
    
    file_status = some_other_function(arg1, arg2) # returns a nested dictionary of lists.
    # In case of next iterations we will skip first element. In case of bigger generators use itertools.islice 
    items = file_status['BU'][1:] if not_first_iteration else file_status['BU']
    for item in items: # file_status['BU'] = ["HR", "DEV" ,"Admin"]
        if item['extensions'] in item_list: #rpm or exe or zip
            if (file_status['status'] == "PUSHED")):
                print("file from {} reached target..".format(item))
                #do further action on that file
            else:
                print("Status of {} is {}".format(item, file_status['status']))
    # To check if all the items in item_list has been pushed.               
    if (file_status_flag and (file_status['status'] == "PUSHED")):
        "All files reached target..make the script successful.."
        extraction_status = False
    first_iteration = True

相关问题