首页 > 解决方案 > 在python中如何比较两个列表,我们可以不区分大小写?

问题描述

如果 current_users 中存在“Maurya”,则不应接受“MAURYA”作为可用用户名。

current_users = ["amit", "ajit", "nishant", "mohit", "Maurya"]
new_users = ["deepak", "manish", "maurya", "akhil", "ajit"]

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user}, you need to enter new username!")
    else:
        print(f"{new_user}, This username is available.")

标签: pythonfor-loopif-statement

解决方案


for new_user in new_users:
    for current_user in current_users:
        if new_user.lower() == current_user.lower():
            print('new user {} allready present'.format(new_user))

推荐阅读