Is there a way to go through a list and check if a given variable matches any of the elements in a list but with the exception that if the found element is not in numerical order, then ignore it and move on. I would like to keep the list as it is so removing elements is not desired.
The list is only part of a complete list that I get when scraping a site. I can get the whole list and then there is no problem but if its possible to do such a check it would save bunch of time.
Round numbers start from X and eventually decreases towards 1 but there can be random numbers in between as shown below. Amount of elements in the list is not guaranteed so it can also vary.
'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'
So for example if I need to find the 9th round, the condition shouldn't be met because the 9th round doesn't come after 10th.
I'm checking for the match but I'm stuck figuring this out.
#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()
CodePudding user response:
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')
OUTPUT
ROUND 23 has been found at 3