我怎样才能混合三个forloop?[Python]

2024-05-08 03:12:28 发布

您现在位置:Python中文网/ 问答频道 /正文

第一个循环:
此循环生成需要更新的单词列表

for text in current_word:
    current_word_text = text.get_attribute('innerHTML')
    current_word_truncated = current_word_text.replace('_', '')
    current_word_length = len(current_word_text)
    current_word_truncated_list = list(current_word_truncated)
    current_word_letter_position = current_word_text.index(
       current_word_truncated) + 1
    words_length_list = [word for word in temp if len(
       word) == current_word_length]

    words_letter_list = [
       word for word in words_length_list if current_word_truncated in word]
    if '_' in current_word_text:
       third_list = [word for word in words_length_list if all(
            letter in word.lower() for letter in current_word_truncated_list)]
       print(third_list)

第二个循环:
此循环使用selenium chrome webdriver将上述单词列表中的单词写入文本区域一段时间

timer = browser.find_elements_by_xpath('//*[@id="timer"]')
for txt in timer:
   for item in third_list:
       third_list = [word for word in words_length_list if all(
            letter in word.lower() for letter in current_word_truncated_list)]
       print(third_list)
       text_input.send_keys(item)
       time.sleep(1)
       text_input.send_keys(u'\ue007')
       timer_text = txt.get_attribute('innerHTML')
       if int(timer_text) == 80:
           break
   timer_text = txt.get_attribute('innerHTML')
   if int(timer_text) == 80:
       break

在第二个循环中,应该始终更新上面的单词列表。这就是为什么我想混合这两个循环,以便将当前单词写入textarea。