如果满足条件,则从第2次迭代开始跳过


#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状态为push,在第一次或迭代中,执行某些操作,从后续迭代开始,如果满足条件,则跳过。

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

等等. .

为什么不添加另一个标志来查看是否已经检查了条件?例如,我们可以添加一个名为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 

我知道这样做没有什么神奇的。但是你可以使用一些额外的变量:

#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

相关内容

  • 没有找到相关文章

最新更新