Python兩個列表元素的對比經常遇到,如何正確的比較?
知識點:列表解析。
詳解:
列表解析 List Comprehensions
表達式:[expression for iter_val in iterable if cond_expr]
- [expression]:最后執行的結果
- [for iter_val in iterable]:這個可以是一個多層循環
- [if cond_expr]:兩個for間是不能有判斷語句的,判斷語句只能在最后;順序不定,默認是左到右。
代碼操作:
current_names = ["java","go","selenium","appium","python"] new_users = ["java","go","javascript","HTTP","C++"] current_names = [current_name.lower() for current_name in current_names] new_users = [new_user.lower() for new_user in new_users] for new_user in new_users: if new_user in current_names: print(new_user + " 用戶名已被使用") else: print(new_user + " 用戶名可以使用")
運行結果:
java 用戶名已被使用
go 用戶名已被使用
javascript 用戶名可以使用
http 用戶名可以使用
c++ 用戶名可以使用
拓展練習:
設置微博名時,常會遇到"用戶名已被占用"情況,那么如何使用Python進行簡單的實現?
設計思路:
1、設置一個列表,用於存儲全網用戶名稱(新名稱則添加、替換名稱則自動刪除)
2、用戶提交名稱時,列表進行一次查詢,查看是否有重復,並做相關提示
3、不區分大小寫,即:PYTHON/Python/python/pYTHOn....默認是一個名字,提交會提醒重復。
代碼如下:
1 """ 2 3 current_names = ["solo", "James", "echo", "JOY", "Tump", "AOBAMA", "新浪新聞", "新浪NEWS", 4 "新浪NBA", "ChinaNEWS"] # 隨意列舉幾個 5 6 Num_Time = 0 #定義編輯次數,大於3次則退出 7 8 while Num_Time <= 3: 9 10 user_name = input("Please input your name:") 11 user_name = user_name.lower() 12 new_users = [] 13 new_users.append(user_name) 14 15 current_names = [current_name.lower() for current_name in current_names] # 列表解析 16 new_users = [new_user.lower() for new_user in new_users] 17 18 if user_name in current_names: 19 print(user_name + "用戶名稱已被占用,請重試。") 20 Num_Time += 1 21 22 else: 23 print(user_name + "用戶名可以使用,請提交。") 24 current_names.append(user_name) 25 print("當前用戶名為:" + user_name) 26 print("用戶名稱列表: " + str(current_names)) 27 break
運行結果:
Please input your name:solo solo用戶名稱已被占用,請重試。 Please input your name:nbs nbs用戶名可以使用,請提交。 當前用戶名為:nbs 用戶名稱列表: ['solo', 'james', 'echo', 'joy', 'tump', 'aobama', '新浪新聞', '新浪news', '新浪nba', 'chinanews', 'nbs'] Process finished with exit code 0
1、如果修改不成功,不計入累積次數
2、如果修改成功,限制次數為3次
3、打印出目前列表
代碼:
1 current_names = ["solo", "James", "echo", "JOY", "Tump", "AOBAMA", "新浪新聞", "新浪NEWS", 2 "新浪NBA", "ChinaNEWS"] # 隨意列舉幾個 3 4 Num_Time = 0 #定義編輯次數,大於3次則退出 5 6 while Num_Time <= 3: 7 8 user_name = input("Please input your name:") 9 user_name = user_name.lower() 10 new_users = [] 11 new_users.append(user_name) 12 13 current_names = [current_name.lower() for current_name in current_names] # 列表解析 14 new_users = [new_user.lower() for new_user in new_users] 15 16 if user_name in current_names: 17 print(user_name + "用戶名稱已被占用,請重試。") 18 # Num_Time += 1 #如果是修改不成功,那么不算修改次數,直接不要累計就行了 19 20 else: 21 print(user_name + "用戶名可以使用,請提交。") 22 current_names.append(user_name) 23 print("當前用戶名為:" + user_name) 24 Num_Time += 1 25 # break 26 if Num_Time >=3: 27 print("每人一年內只能修改3次!")#3次是針對成功修改的操作 28 break 29 30 print("用戶名稱列表: " + str(current_names))
打印結果:
1、如果多次輸入錯誤,不會對累積用影響。
2、成功修改三次后,跳出程序
3、打印出當前列表元素
1 Please input your name:solo 2 solo用戶名稱已被占用,請重試。 3 Please input your name:solo 4 solo用戶名稱已被占用,請重試。 5 Please input your name:solo 6 solo用戶名稱已被占用,請重試。 7 Please input your name:solo 8 solo用戶名稱已被占用,請重試。 9 Please input your name:ka 10 ka用戶名可以使用,請提交。 11 當前用戶名為:ka 12 Please input your name:la 13 la用戶名可以使用,請提交。 14 當前用戶名為:la 15 Please input your name:ji 16 ji用戶名可以使用,請提交。 17 當前用戶名為:ji 18 每人一年內只能修改3次! 19 用戶名稱列表: ['solo', 'james', 'echo', 'joy', 'tump', 'aobama', '新浪新聞', '新浪news', '新浪nba', 'chinanews', 'ka', 'la', 'ji']