求助想找出一组数据的最小值,但是这组数据是不确定个数的
for i in range(1,6):try:
in1=driver.find_element(By.XPATH,f'/html/body/mw/con/table/tbody/tr[{i}]/td').get_attribute('textContent')
in1=in1
match i:
case 1:
one=eval(in1)
case 2:
two=eval(in1)
case 3:
three=eval(in1)
case 4:
four=eval(in1)
case 5:
five=eval(in1)
if one==min(one,two,three,four,five):
print('zhiliao1')
#a=driver.find_element(By.XPATH, f'/html/body/mw/con/table/tbody/tr').get_attribute('onclick')
#driver.execute_script(f'{a}')
elif two==min(one,two,three,four,five):
print('zhiliao2')
elif three==min(one,two,three,four,five):
print('zhiliao3')
elif four == min(one, two, three, four, five):
print('zhiliao4')
elif five == min(one, two, three, four, five):
print('zhiliao5')
except:pass
如上代码,one,two,three,four,five 这五个数据有时候肯能是其中四个找最小值,或者其中三个找最小值,
但是我写的这个代码(if one==min(one,two,three,four,five):)的问题所在是,如果不是五个量都有值的话,就不会运行找最小值 要处理这个问题,你可以在每个循环中创建一个空列表,然后将获取到的值附加到该列表中。这样,你可以在每次循环结束时使用min()函数找到列表中的最小值。以下是代码的修改版本:
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
min_values = []
for i in range(1, 6):
try:
in1 = driver.find_element(By.XPATH, f'/html/body/mw/con/table/tbody/tr[{i}]/td').get_attribute('textContent')
in1 = in1
value = eval(in1)
min_values.append((i, value))
except:
pass
if min_values:
min_item = min(min_values, key=lambda x: x)
min_index = min_item
print(f'zhiliao{min_index}')
else:
print('没有找到最小值')
这个代码会在每次循环中尝试查找元素并提取值。如果成功,它将把这个值添加到min_values列表中。在循环结束后,使用min()函数找到列表中的最小值,并输出对应的"zhiliao"文本。如果没有任何值被找到,程序会输出"没有找到最小值"。 isdkz 发表于 2023-4-25 09:55
要处理这个问题,你可以在每个循环中创建一个空列表,然后将获取到的值附加到该列表中。这样,你可以在每次 ...
谢谢!我刚才用很笨的方法实现了,很臃肿,看完您的解答,我觉得收获很多,并学习新方法去修改原来的代码,感谢!
def minhpcure():
driver.execute_script('useCut(2);return false;')
sleep(1.3)
global one,two,three,four,five
for i in range(1,6):
try:
match i:
case 1:
try:
in1 = driver.find_element(By.XPATH,f'/html/body/mw/con/table/tbody/tr[{i}]/td').get_attribute('textContent')
in1 = in1
one=eval(in1)
except:one=10000
case 2:
try:
in1 = driver.find_element(By.XPATH,f'/html/body/mw/con/table/tbody/tr[{i}]/td').get_attribute('textContent')
in1 = in1
one = eval(in1)
two=eval(in1)
except:two=10000
case 3:
try:
in1 = driver.find_element(By.XPATH,f'/html/body/mw/con/table/tbody/tr[{i}]/td').get_attribute('textContent')
in1 = in1
one = eval(in1)
three = eval(in1)
except:
three = 10000
case 4:
try:
in1 = driver.find_element(By.XPATH,f'/html/body/mw/con/table/tbody/tr[{i}]/td').get_attribute('textContent')
in1 = in1
one = eval(in1)
four = eval(in1)
except:
four = 10000
case 5:
try:
in1 = driver.find_element(By.XPATH,f'/html/body/mw/con/table/tbody/tr[{i}]/td').get_attribute('textContent')
in1 = in1
one = eval(in1)
five=eval(in1)
except:five = 10000
except:pass
def zhiliaomin():
if one==min(one,two,three,four,five):
try:
a=driver.find_element(By.XPATH, f'/html/body/mw/con/table/tbody/tr').get_attribute('onclick')
driver.execute_script(f'{a}')
bskill()
except:pass
elif two==min(one,two,three,four,five):
try:
a=driver.find_element(By.XPATH, f'/html/body/mw/con/table/tbody/tr').get_attribute('onclick')
driver.execute_script(f'{a}')
bskill()
except:pass
elif three==min(one,two,three,four,five):
try:
a = driver.find_element(By.XPATH, f'/html/body/mw/con/table/tbody/tr').get_attribute('onclick')
driver.execute_script(f'{a}')
bskill()
except:pass
elif four == min(one, two, three, four, five):
try:
a = driver.find_element(By.XPATH, f'/html/body/mw/con/table/tbody/tr').get_attribute('onclick')
driver.execute_script(f'{a}')
bskill()
except:pass
elif five == min(one, two, three, four, five):
try:
a = driver.find_element(By.XPATH, f'/html/body/mw/con/table/tbody/tr').get_attribute('onclick')
driver.execute_script(f'{a}')
bskill()
except:pass
页:
[1]