在循环中如何删除列表中的元素?

时间:2020-04-24 08:33:08

标签: python-3.x

我正在循环检查我的“新用户”是否不在我的“当前用户”列表中。如果不是,则将它们添加到当前用户列表中,然后从新用户列表中删除。

检查完成后,我似乎很难删除新用户列表中的元素。在第32或33行中添加行时,没有得到预期的结果。

我的下面的代码:

# Task at hand: Checking Usernames
# create a program that simulates
# how websites ensure that everyone has a unique username.


# HELP NEEDED - Current issue:
# Can't seem to be able to remove the new user once he has been added to the 
# current_user list. 


current_users = ['paul', 'bob', 'robert', 'maria', 'admin']
new_users = ['vali', 'donal', 'aoife', 'robert' , 'gerry']

if current_users:
    if new_users:

        for new_user in new_users:
            if new_user.lower() in current_users:
                print(f'This user already exists. Please chose a new user:  {new_user.title()}')

            else:

            # I also tried replacing "else:" with "if new_user.lower() not 
            # in current_users:" but no luck.

                current_users.append(new_user.lower())
                print(f"Perfect, you have been added to the database: {new_user.title()}")

                # I could not manage to remove the new_user from the list
                # straight from here. 
                # I tried the following 2 'solutions':
                # del new_users[0]
                # new_users.remove(new_user.lower())

    else:
        print("You need new users to check against.")

else:
    print("You need some existing users to check against.")


# Check status of lists: 
print(f"\nCurrent users: {current_users}")
print(f"New users: {new_users}")

3 个答案:

答案 0 :(得分:0)

制作new_users的副本以进行遍历,然后从原始列表中删除:

current_users = ['paul', 'bob', 'robert', 'maria', 'admin']
new_users = ['vali', 'donal', 'aoife', 'robert' , 'gerry']

if current_users:
    if new_users:
        for new_user in new_users.copy():
            if new_user.lower() in current_users:
                print(f'This user already exists. Please chose a new user:  {new_user.title()}')
            else:
                current_users.append(new_user.lower())
                print(f"Perfect, you have been added to the database: {new_user.title()}")
                new_users.remove(new_user)
    else:
        print("You need new users to check against.")
else:
    print("You need some existing users to check against.")

# Check status of lists:
print(f"\nCurrent users: {current_users}")
print(f"New users: {new_users}")

这避免了修改您要遍历的列表的问题。

输出:

Current users: ['paul', 'bob', 'robert', 'maria', 'admin', 'vali', 'donal', 'aoife', 'gerry']
New users: ['robert']

答案 1 :(得分:0)

如果要在迭代新用户列表时对其进行修改,可以通过向后迭代其索引来进行操作。这样,如果您删除当前项目,则仍然需要遍历的索引不会更改。

current_users = ['paul', 'bob', 'robert', 'maria', 'admin']
new_users = ['vali', 'donal', 'aoife', 'robert' , 'gerry']

if current_users:
    if new_users:

        for i in range(len(new_users)-1, -1, -1):  # backward sequence of indices
            if new_users[i].lower() in current_users:
                print(f'This user already exists. Please chose a new user:  {new_users[i].title()}')
            else:
                current_users.append(new_users[i].lower())
                print(f"Perfect, you have been added to the database: {new_users[i].title()}")
                del new_users[i]

    else:
        print("You need new users to check against.")
else:
    print("You need some existing users to check against.")

# Check status of lists: 
print(f"\nCurrent users: {current_users}")
print(f"New users: {new_users}")

输出:

Perfect, you have been added to the database: Gerry
This user already exists. Please chose a new user:  Robert
Perfect, you have been added to the database: Aoife
Perfect, you have been added to the database: Donal
Perfect, you have been added to the database: Vali

Current users: ['paul', 'bob', 'robert', 'maria', 'admin', 'gerry', 'aoife', 'donal', 'vali']
New users: ['robert']

答案 2 :(得分:0)

current_users = ['paul', 'bob', 'robert', 'maria', 'admin']
new_users = ['vali', 'donald', 'aoife', 'robert' , 'gerry']

for current,new in zip(current_users, new_users):
    if new.lower() in current:
        print('This user already exists. Please chose a new user')
        new_users.remove(new)
    else:
        current_users.append(new.lower())
        print("Perfect, you have been added to the database")
        new_users.remove(new)


# Check status of lists: 
print(f"\nCurrent users: {current_users}")
print(f"New users: {new_users}")
相关问题