use*_*098 13 python iteration comparison for-loop list-comparison
我需要一个更好的方法来做到这一点.我是编程的新手,但我知道这是一种非常低效的方法,我需要一个功能,我只是不知道如何完成它.有什么建议?我非常感谢任何帮助!
for H in range(0,len(a_list)):
if a_list[H] > list4[0]:
list5 = [number_list[i]]
if function(list1,list5) == list1[1]:
if function(list2,list5)== list2[1]:
if function(list3,list5)== list3[1]:
if function(list4,list5)== list4[1]:
list5.append(input('some input from the user'))
other_function(list5)
if list5[1]== 40:
print ('something something')
break out of EVERY loop
else:
for H in range(0,len(a_list)):
if a_list[H] > list5[0]:
list6 = [number_list[i]]
if function(list1,list6) == list1[1]:
if function(list2,list6)== list2[1]:
if function(list3,list6)== list3[1]:
if function(list4,list6)== list4[1]:
if function(list5,list6)== list5[1]:
list6.append(input('some input from theuser'))
other_function(list6)
if list6[1]== 40:
print ('something something')
break out of EVERY loop
else:
etc. (one extra comparison every time)
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 13
使用该all()函数测试多个相关条件:
if all(function(lst, list5) == lst[1] for lst in (list1, list2, list3, list4)):
Run Code Online (Sandbox Code Playgroud)
和
if all(function(lst, list6) == lst[1] for lst in (list1, list2, list3, list4, list5)):
Run Code Online (Sandbox Code Playgroud)
像嵌套if语句一样,all()会短路; False任何测试失败后立即返回.
此外,直接循环列表,而不是生成一系列索引.如果您正在使用break,则不需要使用else其中任何一种,删除另一级别的缩进.
您可以通过过滤删除其他级别a_list:
for H in filter(lambda H: H > list4[0], a_list):
Run Code Online (Sandbox Code Playgroud)
这样可以减少您的嵌套:
for H in filter(lambda H: H > list4[0], a_list):
list5 = [number_list[i]]
if all(function(lst, list5) == lst[1] for lst in (list1, list2, list3, list4)):
list5.append(input('some input from the user'))
other_function(list5)
if list5[1]== 40:
print ('something something')
break # out of EVERY loop
for J in filter(lambda J: J >list5[0], a_list):
if all(function(lst, list6) == lst[1] for lst in (list1, list2, list3, list4, list5)):
list6.append(input('some input from theuser'))
other_function(list6)
if list6[1]== 40:
print ('something something')
break # out of EVERY loop
# continue here
Run Code Online (Sandbox Code Playgroud)
想必你的break报表实际使用的异常(raise CustomException()和try:,except CustomException: # break out of all the loops fast),作为一个经常break只会停止当前的循环.
如果要不断添加其他列表和嵌套,可能需要使用列表来保存所有嵌套列表,然后只需添加到外部列表:
class EndLoops(Exception): pass
stack = [[number_list[0]]]
try:
for i in number_list[1:]:
for H in filter(lambda H: H > stack[-1][0], a_list):
stack.append([i])
if all(function(lst, stack[-1]) == lst[1] for lst in stack[:-1]):
stack[-1].append(input('some input from the user'))
other_function(stack[-1])
if stack[-1][1] == 40:
print ('something something')
raise EndLoops
except EndLoops:
pass # broken out of outer loop
Run Code Online (Sandbox Code Playgroud)
突然所有的筑巢都消失了; 相反,您将嵌套移动到stack列表列表.
请注意,我不知道你的代码中最外层循环是什么样的,我只是在黑暗中接受了一次受过教育的刺,但这个想法应该大致正确.
当您有三个或更多编号且使用类似的变量时,请考虑列表。
考虑到这一点,我们首先将 list1、list2、list3...更改为列表的列表(索引为 0,1,2,3 而不是 1,2,3,4)。除非不要称呼它list,因为对于已经有用的东西来说这是一个有用的名称。lst在 Python 中非常流行。我还将把 list5 更改为 lstA,将 list6 更改为 lstB,因为 5 和 6 不再有意义。
现在我们有这个:
for H in range(0,len(a_list)):
if a_list[H] > lst[3][0]:
lstA = [number_list[i]]
if function(lst[0],lstA) == lst[0][1]:
if function(lst[1],lstA)== lst[1][1]:
if function(lst[2],lstA)== lst[2][1]:
if function(lst[3],lstA)== lst[3][1]:
lstA.append(input('some input from the user'))
other_function(lstA)
if lstA[1]== 40:
print ('something something')
break out of EVERY loop
else:
for H in range(0,len(a_list)):
if a_list[H] > lstA[0]:
lstB = [number_list[i]]
if function(lst[0],lstB) == lst[0][1]:
if function(lst[1],lstB)== lst[1][1]:
if function(lst[2],lstB)== lst[2][1]:
if function(lst[3],lstB)== lst[3][1]:
if function(lstA,lstB)== lstA[1]:
lstB.append(input('some input from theuser'))
other_function(lstB)
if lstB[1]== 40:
print ('something something')
break out of EVERY loop
else:
etc. (one extra comparison every time)
Run Code Online (Sandbox Code Playgroud)
现在更明显的是我们基本上做了四次同样的事情。
当你必须多次做同一件事时,请考虑循环。
我们将把块变成循环。我们还将使用标志变量来跟踪测试逻辑时是否出现故障,并使用逻辑“如果不起作用,则跳过某些内容”而不是“如果有效,则执行某些操作”
for H in range(0,len(a_list)):
if a_list[H] > lst[3][0]:
continue #reducing indent levels by negating the check:
#quit on failure instead of work on success
lstA = [number_list[i]]
quit = False
for j in range(4):
if function(lst[j],lstA) != lst[j][1]: #testing FALSEHOOD
quit = True
break #the j loop only
if quit:
continue #reducing indent levels by negating the check
lstA.append(input('some input from the user'))
other_function(lstA)
if lstA[1]== 40:
print ('something something')
break #out of EVERY loop
#else: #don't need the else because we broke
for H in range(0,len(a_list)):
if not a_list[H] > lstA[0]:
continue #reducing indent levels by negating the check
lstB = [number_list[i]]
for j in range(4):
if function(lst[j],lstB) != lst[j][1]: #testing FALSEHOOD
quit = True;
break #to the H loop
if not quit and function(lstA,lstB)== lstA[1]: #combining two checks
lstB.append(input('some input from theuser'))
other_function(lstB)
if lstB[1]== 40:
print ('something something')
break #out of EVERY loop
else: #at this point I'm lost and can't refactor
etc. (one extra comparison every time)
Run Code Online (Sandbox Code Playgroud)
当您必须立即跳出多个循环时,请考虑函数并返回而不是中断。或者异常和 try 块,但有些人可能会觉得这令人讨厌。
失败标志可以工作,但不是很优雅。有一种夸张的说法:“……如果你需要超过 3 级的缩进,那么你就完蛋了,应该修复你的程序。” 可以理解为:如果您有很多级别的缩进(并且某些语言比其他语言需要更多级别),您应该考虑是否可以将一些逻辑移至函数中。
我们还将一些重复的逻辑移至检查器函数中。
(最后,我认为第二个 for 循环嵌套在第一个 for 循环中是一个错误。由于它们具有相同的迭代器变量 H,我认为这会导致无限循环。所以我修复了这个问题。)
#returns FALSE if a check fails, unlike the `quit` variable
def checker(lst, lstA):
for i in range(4):
if function(lst[i],lstA) != lst[i][1]: #testing FALSEHOOD
return False;
return True;
def main(???):
for H in range(0,len(a_list)):
if a_list[H] > lst[3][0]:
continue
lstA = [number_list[i]]
if not checker(lst,lstA):
continue
lstA.append(input('some input from the user'))
other_function(lstA)
if lstA[1]== 40:
print ('something something')
return #break out of EVERY loop
for H in range(0,len(a_list)):
if not a_list[H] > lstA[0]:
continue
lstB = [number_list[i]]
if checker(lst,lstB) and function(lstA,lstB) == lstA[1]:
lstB.append(input('some input from theuser'))
other_function(lstB)
if lstB[1]== 40:
print ('something something')
return # break out of EVERY loop
else: #at this point I'm lost and can't refactor
etc. (one extra comparison every time)
Run Code Online (Sandbox Code Playgroud)