有沒有辦法遍歷串列并檢查給定變數是否與串列中的任何元素匹配,但如果找到的元素不是按數字順序排列的,則忽略它并繼續前進。我想保持串列原樣,因此不需要洗掉元素。
該串列只是我在抓取網站時獲得的完整串列的一部分。我可以獲得整個串列,然后就沒有問題了,但是如果可以進行這樣的檢查,它將節省大量時間。
整數從 X 開始,最終向 1 遞減,但中間可以有亂數,如下所示。串列中的元素數量無法保證,因此它也可能會有所不同。
'ROUND 25'
'ROUND 21'
'ROUND 24'
'ROUND 23'
'ROUND 22'
'ROUND 21'
'ROUND 20'
'ROUND 19'
'ROUND 21'
'ROUND 9'
'ROUND 4'
'ROUND 18'
'ROUND 17'
'ROUND 16'
'ROUND 9'
'ROUND 15'
'ROUND 14'
因此,例如,如果我需要找到第 9 輪,則不應滿足條件,因為第 9 輪不在第 10 輪之后。
我正在檢查比賽,但我一直在弄清楚這一點。
#example list ['ROUND 25', 'ROUND 21', 'ROUND 24', 'ROUND 23', 'ROUND 22', 'ROUND 21', 'ROUND 20', 'ROUND 19', 'ROUND 21', 'ROUND 9', 'ROUND 4', 'ROUND 18', 'ROUND 17', 'ROUND 16', 'ROUND 9', 'ROUND 15', 'ROUND 14']
from_round = '9'
while True:
#fetch rounds on the page
event_round_elements = driver.find_elements_by_class_name("event__round")
# loop through 'event_round_elements' and get the round number by splitting the round '['ROUND 14']'
# then check if any of them matches to the requested 'from_round'
# this is done to determine if more matches need to be loaded
if from_round in [round.text.split(' ')[1].strip() for round in event_round_elements]:
print("Yup, found it: ",from_round)
break
# Didn't find the requested round
# click the 'Show more matches' button
event__more_element.click()
uj5u.com熱心網友回復:
rounds = [
'ROUND 25',
'ROUND 21',
'ROUND 24',
'ROUND 23',
'ROUND 22',
'ROUND 21',
'ROUND 20',
'ROUND 19',
'ROUND 21',
'ROUND 9',
'ROUND 4',
'ROUND 18',
'ROUND 17',
'ROUND 16',
'ROUND 9',
'ROUND 15',
'ROUND 14'
]
# starting loop from round at first position.
# this looks like your input.
find_round = 23
for round_index in range(1, len(rounds)):
# get the current round number.
current_round = rounds[round_index]
current_round_number = int(current_round.split()[1])
# check if cuurent round is round you want to find out.
# and then compare it with previous round.
if current_round_number == find_round:
previous_round = rounds[round_index - 1]
previous_round_number = int(previous_round.split()[1])
if current_round_number == previous_round_number - 1:
print(f'{current_round} has been found at {round_index}')
else:
print('Found But Skipped')
輸出
ROUND 23 has been found at 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/429409.html
上一篇:請求物件未正確過濾
