总共python初学者在这里。Pythonic打破循环的方式
我有以下函数,它检查从某些输入派生的字符串是否存在于文本文件中。它循环遍历文本文件的每一行,以查看是否找到完全匹配。
我必须在找到匹配后立即跳出循环,避免不必要的循环。
下面是代码:
def DateZoneCity_downloaded_previously(Order_Date,ZoneCity): # function to check if a given DateZoneCity
# combination had already been completely downloaded
string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
with open(Record_File) as download_status:
DateZoneCity_exists = False
for line in download_status:
if string_to_match in line:
DateZoneCity_exists = True # if match found, then set "DateZoneCity_exists" to True
break # and break out from the [for line in download_status:] loop
if DateZoneCity_exists: return True
download_status.close()
我正在寻找一个更简洁,Python的方式来构造代码。有什么我可以做得更好吗?为了消除对“DateZoneCity_exists”和第二个If语句的需求?的
这属于[代码评论](https://codereview.stackexchange.com/)。 –
对不起,我应该做些什么?我应该移动线程,我不知道该怎么做。 –
@Chinmay以下是您可能需要考虑的几篇文章:[Stack Overflow用户代码评论指南](http://meta.codereview.stackexchange.com/questions/5777/a-guide-to-code-审查堆栈溢出用户)和[在向代理推荐代码审查时要小心](http://meta.stackoverflow.com/a/260427/4428725) –