I have two distinct columns on a csv file one for numbers (values may occur twice or more times) and the other for links.each row on the file represent a different element
I added them into two lists
numbers=[8,7,9,4.3,4.8,5,........]
Links=["Link1","Link2","Link3",........]
I need to iterate over the elements of the numbers list and if an element is < 5 i need to open the corresponding link (that would be the link placed on the analogous index position on the Links list)
How can i achieve this ? I'm thinking in something like:
For i in numbers If an element in numbers <5 return the index of that element and store to variable inspect
selenium.get Links[inspect]
CodePudding user response:
You can use enumerate()
in a for loop to get both indices and values. A plain for i in numbers:
loop doesn't give you the index of each number, just the number itself, so you can't return the index looping that way. The following code should work:
from selenium import webdriver
driver = webdriver.BrowserName(executable_path = "path/to/driver")
numbers=[8, 7, 9, 4.3, 4.8, 5]
Links= [ "Link1", "Link2", "Link3", "Link4", "Link5", "Link6"]
for index, value in enumerate(numbers):
if value < 5:
driver.get(Links[index])
CodePudding user response:
You can use zip and tuple to make tuples that each one contains a number and a link and then iterate on it:
from selenium import webdriver
web_driver = webdriver.BrowserName(executable_path = "path/to/driver")
for each_element in tuple(zip(numbers, Links)):
if each_element[0] < 5:
web_driver.get(each_element[1])
Or:
from selenium import webdriver
web_driver = webdriver.BrowserName(executable_path = "path/to/driver")
for each_element in [i for i in tuple(zip(numbers, Links)) if i[0] < 5]:
web_driver.get(each_element[1])