2017-12-28 5 views
0

現在のユーザーのリストを現在のユーザーのリストと比較し、重複する名前がないかどうかを確認します。現在のユーザー名を新しいユーザー名と比較し、大文字と小文字を区別しない重複がないことをチェックします(Python3)

これは私がこれまで行ってきたことですが、動作しますが、current_usersはすでに小文字であると仮定しています。

current_users = ['samantha', 'albert', 'amanda', 'dick', 'becky', 'alfonso'] 

new_users = ['AMANDA', 'juan', 'albert', 'alexandra', 'sara', 'raheem'] 

for new_user in new_users: 
    if new_user.lower() in current_users: 
     print("Sorry! This username is taken!") 
    else: 
     print("You are welcome to use this name!") 

私の質問は:リスト全体を書き換えることなく、小文字にcurrent_users内のすべての要素を変換するクリーンな方法は何ですか

おかげ

答えて

0

あなたがこの試すことができます?!

current_users = ['samantha', 'albert', 'amanda', 'dick', 'becky', 'alfonso'] 

new_users = ['AMANDA', 'juan', 'albert', 'alexandra', 'sara', 'raheem'] 

val = {True:'Sorry! This username is taken!', 
     False:'You are welcome to use this name!'} 

arr = [val[True] if new_user.lower() in current_users 
     else val[False] for new_user in new_users ] 

print '\n'.join(arr) 

出力:

Sorry! This username is taken! 
You are welcome to use this name! 
Sorry! This username is taken! 
You are welcome to use this name! 
You are welcome to use this name! 
You are welcome to use this name! 

リストの内包と辞書を使用すると便利です。

関連する問題