[英]Pythonic way to break out of loop
Total python beginner here. 总蟒蛇初学者在这里。
I have the following function, which checks if a string derived from certain inputs exists in a text file. 我有以下函数,它检查文本文件中是否存在从某些输入派生的字符串。 It loops through each line of the text file, to see if an exact match is found.
它遍历文本文件的每一行,以查看是否找到了完全匹配。
I have to break out of looping, immediately after the match is found, to avoid needless looping. 我必须在找到匹配后立即退出循环,以避免不必要的循环。
Here is the code: 这是代码:
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()
I am searching for a more concise, pythonic way to structure the code. 我正在寻找一种更简洁,更pythonic的方式来构造代码。 Is there anything I can do to make this better?
有什么办法可以让它变得更好吗? Somehow to eliminate the need for "DateZoneCity_exists" and the second If statement?
以某种方式消除了对“DateZoneCity_exists”和第二个If语句的需求?
This feels like a situation where any
would be the best solution: 这感觉就像
any
一种最好的解决方案:
# Function to check if a given DateZoneCity
def DateZoneCity_downloaded_previously(Order_Date, ZoneCity):
# 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:
return any((string_to_match in line) for line in download_status)
Note that in this case it will return False
on negative rather than your current implementation that will return None
, also note that it does break out of the looping immediately upon finding a positive result so it does not require looping through the entire file either way. 请注意,在这种情况下,它将返回
False
on Negative而不是当前实现将返回None
,同时请注意它在找到肯定结果后立即突破循环,因此它不需要以任何方式循环遍历整个文件。
Just return
instead of break
: 只是
return
而不是break
:
def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):
"""Check if a given DataZoneCity combination had already been downloaded."""
string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
with open(Record_File) as download_status:
for line in download_status:
if string_to_match in line:
return True
return False # No match found.
Depending on how big your text file is, you can read it into a string, and just use that (easier and often faster than reading and checking line per line): 根据文本文件的大小,您可以将其读入字符串,然后使用它(比每行读取和检查行更容易且更快):
if string_to_match in open(Record_File).read():
return True
In your example: 在你的例子中:
def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):
string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
if string_to_match in open(Record_File).read():
return True
You don't have to put a for loop here, See the updated code. 您不必在此处放置for循环,请参阅更新的代码。
def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):
"""Check if a given DataZoneCity combination had already been downloaded."""
string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
with open(Record_File) as download_status:
if string_to_match in download_status.read():
return True
return False # No match found.
If record_file is small then you can use in
directly with if
statement like:- 如果record_file较小,则可以用
in
直接if
语句,如: -
def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):
string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
if string_to_match in open(Record_File).read():
return True
while if record_file is large then you have to iterate through each line for finding a match like:- 如果record_file很大,那么你必须遍历每一行才能找到匹配,如: -
def DateZoneCity_downloaded_previously(Order_Date, ZoneCity):
string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
with open(Record_File) as download_status:
return is_empty((string_to_match in line) for line in download_status)
because reading line by line will save storage memory used for saving line. 因为逐行读取会节省用于保存行的存储空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.