35 字典中 format_map方法格式化字符串序列與迭代 clear copy deepcopy

第四課 用format_map方法格式化字符串 用於字典中 # 用format_map方法格式化字符串 首先 format_map方法 不是字典中的,是字符串的 values1 = (1,2,'hello') # 這是定義一個元組 str1 = "abc %d xyz, %d, %s world" # %d 數字 %s 字符串 print(str1 % values1) # abc 1 xyz, 2, hello world values2 = {'title':'極客起源', 'url':'https://geekori.com','company':'歐瑞科技'} # 定義一個字典 str2 = """ <html> <head> <title>{title}</title> <meta charset="utf-8"/> </head> <body> <h1>{title}</h1> <a href="{url}">{company}</a> </body> </html> """ print(str2.format_map(values2)) # 輸出的結果爲 ''' <html> <head> <title>極客起源</title> <meta charset="utf-8"/> </head> <body> <h1>極客起源</h1> <a href="https://geekori.com">歐瑞科技</a> </body> </html> ''' ---------------------------------------------- 第五課 序列(列表 元組 字典)與迭代 # 序列與迭代 序列包含(列表 元組 字典) 迭代 可以比如重複的for循環 ''' 1. 獲取字典中key的列表 2. 獲取字典中key—value對的列表 3. 並行迭代 4. 壓縮序列 5. 反轉序列迭代 迭代是重複反饋過程的活動,其目的通常是爲了逼近所需目標或結果。每一次對過程的重複稱爲一次“迭代”,而每一次迭代得到的結果會作爲下一次迭代的初始值。 重複執行一系列運算步驟,從前面的量依次求出後面的量的過程。此過程的每一次結果,都是由對前一次所得結果施行相同的運算步驟得到的。例如利用迭代法*求某一數學問題的解。 對計算機特定程序中需要反覆執行的子程序*(一組指令),進行一次重複,即重複執行程序中的循環,直到滿足某條件爲止,亦稱爲迭代。 ''' # 定義一個字典 d = {"name":"Bill", "age":20,"sex":"男", "salary":4567.5} # 迭代字典中的key for key in d: print("{} = {}".format(key, d[key]), end = ' ') # name = Bill age = 20 sex = 男 salary = 4567.5 print() # 同時迭代字典中的key和value for key,value in d.items(): # d.items() 字典中的一個 items方法 獲取字典中的kv對 print("{} = {}".format(key, value), end = ' ') # name = Bill age = 20 sex = 男 salary = 4567.5 # 上面的2個結果是完全一樣的,方法和思想是不一樣的 print() # 換行 # 並行迭代 list1 = [1,2,3,4,5] list2 = ['a','b','c','d','e'] for i in range(len(list1)): # 產生一個範圍 print("list1[{}] = {}, list2[{}] = {}".format(i,list1[i],i,list2[i]),end = " ") # 輸出的結果Wie # list1[0] = 1, list2[0] = a list1[1] = 2, list2[1] = b list1[2] = 3, list2[2] = c list1[3] = 4, list2[3] = d list1[4] = 5, list2[4] = e print() # 壓縮迭代 引入zip函數 for value in zip(list1, list2): print(value, end = " ") # (1, 'a') (2, 'b') (3, 'c') (4, 'd') (5, 'e') print() items = [] # 定義一個列表之後, 追加 然後進行 dict轉化爲 字典 for value in zip(list2, list1): items.append(value) d1 = dict(items) print(d1) # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} print() # print(zip(list1, list2)) # 反轉排序迭代 這裏values1的元素 只支持數字,不支持字符串的 values1 = [4,1,5,6,3,1,7,9] print(sorted(values1)) # [1, 1, 3, 4, 5, 6, 7, 9] sorted 排序 values2 = reversed(values1) # reversed 反轉 for v in values2: print(v, end = " ") # 9 7 1 3 6 5 1 4 print() # abcdefg gfedcba print(''.join(list(reversed("abcdefg")))) # 結果爲: gfedcba 思想爲:reversed先倒序 然後list轉化爲一個列表 然後在通過 join鏈接起來 中間沒有任何的東西,需要用空串去鏈接就可以了 最後的結果爲就是把 列表中的元素首尾 相接 中間沒有任何的分隔符 2個函數1個方法 分佈式爲: print(list(reversed("abcdefg"))) # ['g', 'f', 'e', 'd', 'c', 'b', 'a'] ------------------------------------------------ 第六課 清空字典(clear方法) # clear方法: 徹底清空字典 names1 = {"Bill":20, "Mike":30,"John":50} names2 = names1 names1["Bill"] = 45 print(names2) # {'Bill': 45, 'Mike': 30, 'John': 50} #names1 = {} names1.clear() # 清空 print(names2) # {} ----------------------------------------------- 第七課 複製字典(copy方法和deepcopy函數) # copy方法與deepcopy函數 2個都是複製字典的 但是他們有什麼區別呢? # copy方法是潛複製 只複製第一層 剩下的就共同指向一個值了 如果字典中的值是另外的一個字典或者一個列表 複製完的字典的值就是指向同一個對象 通俗的講究是 變一個都變 相互關聯的 # deepcopy函數 深複製 不管你有多少層都進行復制 變其中一個,另外的一個不變 person1 = {"name":"Bill", "age":30, "fullName":["Bill","Gates"]} person2 = person1.copy() print('person2' , person2) # person2 {'name': 'Bill', 'age': 30, 'fullName': ['Bill', 'Gates']} 結果和person1 是完全一樣的 person1["age"] = 60 # 改變了 person1的值,person1 變了 person2 是person1 copy過來的 那麼person2 的值 沒有變 print('person1' , person1) # person1 {'name': 'Bill', 'age': 60, 'fullName': ['Bill', 'Gates']} print('person2' , person2) # person2 {'name': 'Bill', 'age': 30, 'fullName': ['Bill', 'Gates']} person1["fullName"][1] = "Clinton" # 改變了 fullName (爲第二層) 索引爲1的值 person1 person2 的值都變了 這一步爲潛複製 變一個都變 print('person1' , person1) # person1 {'name': 'Bill', 'age': 60, 'fullName': ['Bill', 'Clinton']} print('person2' , person2) # person2 {'name': 'Bill', 'age': 30, 'fullName': ['Bill', 'Clinton']} from copy import deepcopy person1 = {"name":"Bill", "age":30, "fullName":["Bill","Gates"]} person2 = deepcopy(person1) # 在這裏 person1 和person2 爲2個完全不同的2個字典 改變了person1 的值 person2的值是不會變的 person1["fullName"][1] = "Clinton" # 用 deepcopy函數 改變了 fullName 索引爲1的值 person1 變了 person2 沒有變 print("person1", person1) # person1 {'name': 'Bill', 'age': 30, 'fullName': ['Bill', 'Clinton']} print("person2", person2) # person2 {'name': 'Bill', 'age': 30, 'fullName': ['Bill', 'Gates']} --------------------------------------------- 第八課 根據key創建字典(fromkeys方法) # fromkeys方法 : 作用 可以根據這個方法 key 創建一個字典 並且所有的key都是擁有同一個值,同一個默認值 這個方法的返回值就是新的字典 # None newDict1 = {}.fromkeys(['name', 'company', 'salary']) # 空的字典,返回值的value爲空 用列表表示key print(newDict1) # {'name': None, 'company': None, 'salary': None} newDict2 = newDict1.fromkeys(('name', 'company','age')) # 空的字典,返回值的value爲空 用元組表示key print(newDict2) # {'name': None, 'company': None, 'age': None} newDict3 = newDict1.fromkeys(['name', 'company', 'salary'],'沒有值') # 不指定就是None print(newDict3) # {'name': '沒有值', 'company': '沒有值', 'salary': '沒有值'} newDict3 = {}.fromkeys(['name', 'company', 'salary'],'majihui') # {'name': 'majihui', 'company': 'majihui', 'salary': 'majihui'} print(newDict3) ----------------------------------------------- 第九課 用更寬鬆的方式獲取字典中的 value(get方法) # get方法 dict = {"name":"Bill", "age":30} print(dict["age"]) # 30 #dict['salary'] = 3000 #print(dict['salary']) # 報錯 不存在 print(dict.get('salary')) # None 用get的話 不存在的值 會返回 None print(dict.get('salary', 3000)) # 3000 # 此場景經常用於解讀 key values的值 get方法去獲取不在字典中的key 友好的輸出不至於報錯 d = {'help':'幫助', 'bike':'自行車','geek':'極客', 'China':'中國'} while True: word = input('請輸入英文單詞:') if word == ':exit': break; value = d.get(word) if value == None: print('{}在字典中不存在.'.format(word)) else: print('"{}"的含義是"{}"'.format(word,value)) 請輸入英文單詞:china china在字典中不存在. 請輸入英文單詞:China "China"的含義是"中國" 請輸入英文單詞:exit
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章