-->將資料值儲存在鍵:值對
中
-->有序、可更改且不允許重複。
練習:
1.
menu = {'idli':10, 'dosai':20, 'poori':30} print(menu) menu['pongal'] = 40 del menu['idli'] print(menu) print(menu['dosai'])
輸出:
{'idli': 10, 'dosai': 20, 'poori': 30} {'dosai': 20, 'poori': 30, 'pongal': 40} 20
2。要在空字典中加入鍵:值對,使用 get() 函數
time_table = {} time_table['tamil'] = 10 time_table['english']= 10 print(time_table) print(time_table['tamil']) print(time_table.get('tamil')) print(time_table.get('maths')) print(time_table['maths'])
輸出:
{'tamil': 10, 'english': 10} 10 10 None KeyError: 'maths'
-->如果通常我們輸入字典中沒有的鍵,那麼輸出將是 KeyError。
-->相反,如果我們使用 get() 函數,它不會傳回任何內容。
3.取得鍵、值、兩者
menu = {'idli':10, 'dosai':20, 'poori':30} print(menu) print(menu.keys()) print(menu.values()) print(menu.items())
輸出:
{'idli': 10, 'dosai': 20, 'poori': 30} dict_keys(['idli', 'dosai', 'poori']) dict_values([10, 20, 30]) dict_items([('idli', 10), ('dosai', 20), ('poori', 30)])
Keys-->從字典列印鍵(dict_name.keys())。
值-->列印字典中的值(dict_name.values())。
items-->將字典中的鍵和值列印為元組 (dict_name.items())。
4。從字典中單獨取得鍵、值或項目。
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} for fruit in fruits_menu.keys(): print(fruit) for price in fruits_menu.values(): print(price) for fruit, price in fruits_menu.items(): print(fruit, price)
輸出:
apple banana grapes 100 80 120 apple 100 banana 80 grapes 120
5。如果鍵包含字母“e”,則列印值。
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} for fruit in fruits_menu.keys(): if 'e' in fruit: print(fruits_menu[fruit])
輸出:
100 120
6。將字典轉換為元組或列表。
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} print(list(fruits_menu)) print(tuple(fruits_menu))
輸出:
['apple', 'banana', 'grapes'] ('apple', 'banana', 'grapes')
7。嵌套字典。
emp1 = {'name':'guru prasanna', 'qual':'B.Com'} emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'} print(emp1) print(emp2) employees = {101:emp1, 102:emp2} print(employees)
輸出:
{'name': 'guru prasanna', 'qual': 'B.Com'} {'name': 'lakshmi pritha', 'qual': 'M.E'} {101: {'name': 'guru prasanna', 'qual': 'B.Com'}, 102: {'name': 'lakshmi pritha', 'qual': 'M.E'}}
8。從字典中單獨取得員工姓名。
emp1 = {'name':'guru prasanna', 'qual':'B.Com'} emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'} employees = {101:emp1, 102:emp2} print(employees) for roll_no, employee in employees.items(): for key, value in employee.items(): if key == 'name': print(employee[key])
輸出:
{101: {'name': 'guru prasanna', 'qual': 'B.Com'}, 102: {'name': 'lakshmi pritha', 'qual': 'M.E'}} guru prasanna lakshmi pritha
9。單獨取得 B.com 員工。
emp1 = {'name':'guru prasanna', 'qual':'B.Com'} emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'} employees = {101:emp1, 102:emp2} for roll_no, employee in employees.items(): for key, value in employee.items(): if value == 'B.Com': print(employee['name'])
輸出:
guru prasanna
10。每個值增加 10%。
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} for fruit in fruits_menu.values(): fruit=fruit+(fruit/10) print(fruit)
輸出:
110.0 88.0 132.0
11。轉換鍵-->價值與價值-->鍵。
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} new_menu = {} for fruit,price in fruits_menu.items(): new_menu[price] = fruit print(new_menu)
輸出:
{100: 'apple', 80: 'banana', 120: 'grapes'}
字典理解
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} menu_dict = {(fruit,price) for fruit,price in fruits_menu.items()} print(menu_dict) menu_dict = {fruit: price for fruit,price in fruits_menu.items()} print(menu_dict) #To reverse key-->value and value-->key menu_dict = {price : fruit for fruit,price in fruits_menu.items()} print(menu_dict)
輸出:
{('grapes', 120), ('apple', 100), ('banana', 80)} {'apple': 100, 'banana': 80, 'grapes': 120} {100: 'apple', 80: 'banana', 120: 'grapes'}
get()
get() 方法傳回具有指定鍵的項目的值。
-->如果字典中不存在鍵,則傳回無。
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} print(fruits_menu.get('apple',"not available")) print(fruits_menu.get('kiwi',"not available"))
輸出:
100 not available
找出給定字串中字母的頻率
#frequency of each letter in a given string freq = {} name = 'guruprasanna' for letter in name: freq[letter] = freq.get(letter,0)+1 print(freq)
輸出:
{'g': 1, 'u': 2, 'r': 2, 'p': 1, 'a': 3, 's': 1, 'n': 2}
將字典轉換為集合
csk = {'dhoni':101, 'jadeja':102} india = {'virat':103, 'jadeja':102} print(set(csk)) print(set(india)) print(set(csk.keys())) print(set(india.keys()))
輸出:
{'dhoni', 'jadeja'} {'virat', 'jadeja'} {'dhoni', 'jadeja'} {'virat', 'jadeja'}
setdefault()
--> setdefault() 方法傳回具有指定鍵的項目的值。
-->如果鍵不存在,則插入具有指定值的鍵。
csk = {'dhoni':101, 'jadeja':102} india = {'virat':103, 'jadeja':102} csk.setdefault('rohit',100) print(csk) csk.setdefault('dhoni',100) print(csk)
輸出:
menu = {'idli':10, 'dosai':20, 'poori':30} print(menu) menu['pongal'] = 40 del menu['idli'] print(menu) print(menu['dosai'])
1。尋找:
a) 兩個團隊都有共同點
b) 出現在任何一支球隊
c) 總玩家姓名
{'idli': 10, 'dosai': 20, 'poori': 30} {'dosai': 20, 'poori': 30, 'pongal': 40} 20
輸出:
time_table = {} time_table['tamil'] = 10 time_table['english']= 10 print(time_table) print(time_table['tamil']) print(time_table.get('tamil')) print(time_table.get('maths')) print(time_table['maths'])
2。找出字串中單字的出現頻率 - 'a Rose is a Rose is a Rose'。
{'tamil': 10, 'english': 10} 10 10 None KeyError: 'maths'
輸出:
menu = {'idli':10, 'dosai':20, 'poori':30} print(menu) print(menu.keys()) print(menu.values()) print(menu.items())
3。在字典中找出總分、平均分數和高分。
玩家 = {'jaiswal':75, 'rohit':55, 'virat':95}
{'idli': 10, 'dosai': 20, 'poori': 30} dict_keys(['idli', 'dosai', 'poori']) dict_values([10, 20, 30]) dict_items([('idli', 10), ('dosai', 20), ('poori', 30)])
輸出:
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} for fruit in fruits_menu.keys(): print(fruit) for price in fruits_menu.values(): print(price) for fruit, price in fruits_menu.items(): print(fruit, price)
apple banana grapes 100 80 120 apple 100 banana 80 grapes 120
輸出:
fruits_menu = {'apple':100, 'banana':80, 'grapes':120} for fruit in fruits_menu.keys(): if 'e' in fruit: print(fruits_menu[fruit])
以上是Python Day-Dictionary-練習、任務的詳細內容。更多資訊請關注PHP中文網其他相關文章!