3. 使用列表/集合/字典

tags: Python

以下程式碼無先後順序 無連貫性

推薦影片教學頻道:

一、列表的使用

# 定義三個列表 一個字串 gg = "i am here~" courses1 = ["Math", "History", "Physics", "Compute"] courses2 = ["Art", "Life"] nums = [1, 5, 10, 8, 7, 2] #使用列表 # 取得列表長度 len(courses1) #取得courses1列表的長度 # 新增物件 courses1.append(gg) #新增gg到courses列表中 # ["Math", "History", "Physics", "Compute", "i am here~"] courses1.insert(2, gg) #將gg插入到courses1列表中第3格 # ["Math", "History", "i am here~", "Physics", "Compute"] courses1.extend(courses2) #將courses2列表中的物件加到courses1中 # ["Math", "History", "Physics", "Compute", "Art", "Life"] courses1.append(courses2) #將courses2整個列表加入倒courses1中 # ["Math", "History", "Physics", "Compute", ["Art", "Life"]] # 則 "Art" 等於 courses[4][0] => 列表名稱[列表第一層位置][列表第二層位置]. . . . # 整理列表 courses1.sort() # .sort() 會直接更改列表 整理全數字或全字串的列表 #字串=>字母順序 數字=>大小 從第0位置開始比對 # ['Compute', 'History', 'Math', 'Physics'] nums.sort() # [1, 2, 5, 7, 8, 10] nums.sort(reverse=True) # 可以使用 reverse=True 讓整理好的列表順序顛倒 # [10, 8, 7, 5, 2, 1] courses1.remove("Math") #從列表中移除一個物件 此例終將被移除的物件為 "Math" # ['History', 'Physics', 'Compute'] courses1.pop(2) #移除列表第三個物件 # ['Math', 'History', 'Compute'] max(nums) #數字列表中 最大的值 => 10 min(nums) #數字列表中 最小的值 => 1 courses1.index("History") #取得 "History" 在列表中的位置 => 1 string = " - ".join(courses1) #將列表攤平變成字串 以 " - " 分隔原先列表中各個物件 # "Math - History - Physics - Compute"

二、集合的使用

#定義兩個列表 language = ["Taiwanese", "English", "Chinese", "Japanese"] I_can_say = ["Taiwanese", "English", "Taiwanese", "Portuguese"] #集合的使用 # 1. 將列表轉換為集合 language = set(language) I_can_say = set(I_can_say) # 2-1. 判斷物件是否存在 使用列表List print("Taiwanese" in I_can_say) #會輸出True => True 因為 "Taiwanese" 在列表中 #但是 這樣會跑過所有物件來判斷 而集合的不重複性會讓判斷速度優化 # 2-2. 判斷物件是否存在 使用集合Set print("Taiwanese" in set(I_can_say)) #會輸出True => True 因為 "Taiwanese" 在集合中 #將 I_can_say列表轉為Set集合後判斷 "Taiwanese" 是否在集合中 # 3-1. 對比列表物件 共同傭有物件 print(set(language).intersection(set(I_can_say))) # => {"Taiwanese", "English"} # 3-2. 對比列表物件 查看不同物件 => a.difference(b) => 會輸出 a有 但b沒有的 print(set(language).difference(set(I_can_say))) # => {"Japanese"} # 3-3. 對比列表物件 合併兩個集合中的物件 共同擁有的只會保留一個 保持集合的不重複性 print(set(language).union(set(I_can_say))) # {"Taiwanese", "English", "Chinese", "Japanese", "Portuguese"}

三、字典的使用

# 例子 1 dictionary = {"a":"apple", "b":"banana", 1:"this is 1", 2:"this is 2", "2":2} # 名稱 -- 數值 # "a" -- "apple" # "b" -- "banana" # 1 -- "this is 1" # 2 -- "this is 2" # "2" -- int(2) #如果我想要取得整數 2 dictionary["2"] #想要取得 "apple" dictionary["a"] #想要取得 "this is 2" dictionary[2] #如果想要找一個值 但不確定存不存在 => dictionary.get(想找的key, 如果不存在的話輸出甚麼) dictionary.get("Am I Handsome?", "Not Found") #!!!進階例子!!! students = { "John" : { "name" : "John", "age" : 17, "gender" : "male", "working" : False, "habbits" : ["basket ball", "soccer", "tennis"], "grade" : "second-grade" }, "Alice" : { "name" : "Alice", "age" : 16, "gender" : "female", "working" : False, "habbits" : ["chess", "tennis"], "grade" : "first-grade" }, "Keven" : { "name" : "Keven", "age" : 18, "gender" : "male", "working" : True, "habbits" : ["basket ball", "tennis", "jogging"], "grade" : "third-grade" }, "Jarvis" : { "name" : "Jarvis", "age" : 18, "gender" : "male", "working" : True, "grade" : "third-grade" } } #將 students 字典第一層的key轉為列表一一提取到變數name for name in students.keys(): #如果 students 中的 key等於name 所對應到的 數值(字典) 中有"habbits"這個key的話 if "habbits" in students[name].keys(): print(name, "habbits", True) # 輸出學生名字並輸出True else: print(name, "habbits", False) # 否則輸出學生名字並輸出False]
進階例子中 我們舉例有四位學生 John Alice Keven Jarvis
用字典 並用下面提供的迴圈 (下一篇會詳細介紹)
就可以尋找是否有人缺少資料
此例子中 學生 Jarvis 缺少興趣資料 habbits
所以會輸出 Jarvis habbits False

上一篇

下一篇