如何使用while循环删除列表元素

时间:2018-05-12 08:34:22

标签: python-3.x python-2.7 for-loop if-statement while-loop

我打开一个网页来获取cluster1的名称(p1,p2)。我不确定我需要多少次打开一个网页来获取这些cluster1名称。所以,我使用的是while循环,它将删除从网页获取值的p1或p2。

当我打开网页时,我会得到p1或p2,该值将存储在新的[-1]中。如果此值在cluster1中,它将执行其他测试函数,并且该值将从cluster1中删除。

new = ['some',  'list' 'items', 'p1'] # last element of list is either p1 or p2. So, new[-1] will give p1 or p2.

cluster1 =[ 'p1', 'p2'] 

 while len(cluster1) != 0:
    print("Length of cluster1 before:", len(cluster1))
    # for i in range(10):

    if new[-1] in cluster1:
        print(new[-1] + " is in cluster1.")
        test1()
        test2()
        new_ver_names.append(new[-1])
        cluster1.remove(new[-1])
        print("Length of cluster1 after:", len(cluster1))
    else:
            print(new[-1] + " portal version is not listed.")
            driver.quit()
            break

我的例外是,当删除值时,控制应该返回while循环并再次启动,直到len(cluster1)为0.并且,如果该值不在cluster1中,则其他应该执行。但是,当我删除cluster1.remove(new[-1])时,其他部分也会被执行。

我检查了其他答案,其中提到我们无法从列表中删除项目,同时迭代它并尝试列表理解。但是,无法使其发挥作用。

我尝试了lst = [(teset1(), test2()) for i in range(len(cluster1)) if new[-1] in cluster1]

非常感谢任何帮助。

谢谢。

编辑:

def login1():

ChromeDriver = 'C:\\PortalTesting\\Drivers\\chromedriver.exe'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--incognito")

chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(executable_path=ChromeDriver, chrome_options=chrome_options)
driver.maximize_window()
driver.implicitly_wait(130)

driver.get("MY_URL")
print("session id ", driver.session_id)
username = driver.find_element_by_css_selector("#uid")
username.send_keys("username")
password = driver.find_element_by_css_selector("#pid")
password.send_keys("password")

login_button = driver.find_element_by_class_name("secondarybtnlabel")
login_button.click()
# time.sleep(10)
cluster1 = ['p1', 'p2']
dc_elm = driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/span[2]').text
new = unicodedata.normalize('NFKD', dc_elm).encode('ascii', 'ignore').split()
print("Portal version: ", new[-1])
logout_btn = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[4]/div/div[2]/div/header/div[2]/table/tbody/tr/td[3]/div/li')
logout_btn.click()

driver.delete_all_cookies()
print("Clearing cookies")
new_ver_names = []
time.sleep(3)

while len(cluster1) != 0:
    print("Length of cluster1 before:", len(cluster1))
    # for i in range(10):
    if new[-1] in cluster1:
        print(new[-1] + " is in cluster1.")
        test1()
        test2()

        new_ver_names.append(new[-1])

        # cluster1.remove(new[-1])
        print("Length of cluster1 after:", len(cluster1))
    else:
            print(new[-1] + " portal version is not listed.")
            driver.quit()
            break
        # cluster1.remove(new[-1])

1 个答案:

答案 0 :(得分:1)

很难(对我来说)了解当前代码中发生了什么,但为什么不尝试set

new = {'some',  'list' 'items', 'p1'}
cluster1 ={'p1', 'p2'}

in_both = new & cluster1
not_found = cluster1 - new

# do stuff with values in `in_both` and `not_found`....