[英]Is it possible to ignore the last element of a list without having to create another list and without modifing the original list?
[英]it is possible to ignore the last element of the list?
我有 2 個有工作時間和工作要做的清單
hours = ["00:00","06:00","08:30","09:30","10:30","12:30","14:30","16:00","18:00",]
jobs = [job1,job2,job3,job4,job5,job6,job7,job8,job9]
帶有一個循環來使用調度庫來調度作業。
for i,x in zip(hours,jobs):
schedule.every().monday.at(i).do(x)
schedule.every().tuesday.at(i).do(x)
schedule.every().wednesday.at(i).do(x)
schedule.every().thursday.at(i).do(x)
schedule.every().friday.at(i).do(x)
while True:
schedule.run_pending()
time.sleep(1)
我想做的是在星期五schedule.every().friday.at(i).do(x)
我安排除了最后一個以外的所有工作
job9 at 18:00
在這種情況下,有什么辦法可以避免列表中的最后一項?
您可以添加一個if
條件來檢查它是否位於列表的最后一個元素:
for i,x in zip(hours,jobs):
if i = hours[-1]:
break
schedule.every().monday.at(i).do(x)
schedule.every().tuesday.at(i).do(x)
schedule.every().wednesday.at(i).do(x)
schedule.every().thursday.at(i).do(x)
schedule.every().friday.at(i).do(x)
這會繼續循環,直到它到達最后一個元素( hours[-1]
),它會跳出循環。
我相信伊斯梅爾的回答將避免在所有日子安排最后一份工作。 如果您只希望星期五受到影響,請在星期五之前放置if
語句:
for i,x in zip(hours,jobs):
schedule.every().monday.at(i).do(x)
schedule.every().tuesday.at(i).do(x)
schedule.every().wednesday.at(i).do(x)
schedule.every().thursday.at(i).do(x)
if i != hours[-1]:
schedule.every().friday.at(i).do(x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.