字典 – {}:
字典存储数据,有序、可变且键值唯一。
练习:
- 字典的基本操作:
menu = {'idli': 10, 'dosai': 20, 'poori': 30} print(menu) # 输出:{'idli': 10, 'dosai': 20, 'poori': 30} menu['pongal'] = 40 del menu['idli'] print(menu) # 输出:{'dosai': 20, 'poori': 30, 'pongal': 40} print(menu['dosai']) # 输出:20
登录后复制
- 使用 get() 函数添加键值对:
time_table = {} time_table['tamil'] = 10 time_table['english'] = 10 print(time_table) # 输出:{'tamil': 10, 'english': 10} print(time_table['tamil']) # 输出:10 print(time_table.get('tamil')) # 输出:10 print(time_table.get('maths')) # 输出:None #print(time_table['maths']) # 抛出 KeyError: 'maths'
登录后复制
get() 函数在键不存在时返回 None,避免 KeyError 异常。
立即学习“”;
- 获取键、值和键值对:
menu = {'idli': 10, 'dosai': 20, 'poori': 30} print(menu) # 输出:{'idli': 10, 'dosai': 20, 'poori': 30} print(menu.keys()) # 输出:dict_keys(['idli', 'dosai', 'poori']) print(menu.values()) # 输出:dict_values([10, 20, 30]) print(menu.items()) # 输出:dict_items([('idli', 10), ('dosai', 20), ('poori', 30)])
登录后复制
- 遍历字典:
fruits_menu = {'apple': 100, 'banana': 80, 'grapes': 120} for fruit in fruits_menu.keys(): print(fruit) # 输出:apple banana grapes for price in fruits_menu.values(): print(price) # 输出:100 80 120 for fruit, price in fruits_menu.items(): print(fruit, price) # 输出: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]) # 输出:100 120
登录后复制
- 字典转换为列表或元组:
fruits_menu = {'apple': 100, 'banana': 80, 'grapes': 120} print(list(fruits_menu)) # 输出:['apple', 'banana', 'grapes'] print(tuple(fruits_menu)) # 输出:('apple', 'banana', 'grapes')
登录后复制
- 嵌套字典:
emp1 = {'name': 'guru prasanna', 'qual': 'b.com'} emp2 = {'name': 'lakshmi pritha', 'qual': 'm.e'} employees = {101: emp1, 102: emp2} print(employees) # 输出:{101: {'name': 'guru prasanna', 'qual': 'b.com'}, 102: {'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 key == 'name': print(employee[key]) # 输出:guru prasanna lakshmi pritha
登录后复制
- 查找特定条件的嵌套字典值:
for roll_no, employee in employees.items(): for key, value in employee.items(): if value == 'b.com': print(employee['name']) # 输出:guru prasanna
登录后复制
- 修改字典值:
fruits_menu = {'apple': 100, 'banana': 80, 'grapes': 120} for fruit in fruits_menu: fruits_menu[fruit] = fruits_menu[fruit] + fruits_menu[fruit] * 0.1 print(fruits_menu) # 输出:{'apple': 110.0, 'banana': 88.0, 'grapes': 132.0}
登录后复制
- 键值对互换:
fruits_menu = {'apple': 100, 'banana': 80, 'grapes': 120} new_menu = {price: fruit for fruit, price in fruits_menu.items()} 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) # 输出:{('grapes', 120), ('apple', 100), ('banana', 80)} menu_dict = {fruit: price for fruit, price in fruits_menu.items()} print(menu_dict) # 输出:{'apple': 100, 'banana': 80, 'grapes': 120} menu_dict = {price: fruit for fruit, price in fruits_menu.items()} print(menu_dict) # 输出:{100: 'apple', 80: 'banana', 120: 'grapes'}
登录后复制
get() 方法:
fruits_menu = {'apple': 100, 'banana': 80, 'grapes': 120} print(fruits_menu.get('apple', "not available")) # 输出:100 print(fruits_menu.get('kiwi', "not available")) # 输出:not available
登录后复制
统计字母频率:
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)) # 输出:{'jadeja', 'dhoni'} print(set(india)) # 输出:{'jadeja', 'virat'} print(set(csk.keys())) # 输出:{'jadeja', 'dhoni'} print(set(india.keys())) # 输出:{'jadeja', 'virat'}
登录后复制
setdefault() 方法:
csk = {'dhoni': 101, 'jadeja': 102} csk.setdefault('rohit', 100) print(csk) # 输出:{'dhoni': 101, 'jadeja': 102, 'rohit': 100} csk.setdefault('dhoni', 100) print(csk) # 输出:{'dhoni': 101, 'jadeja': 102, 'rohit': 100}
登录后复制
任务:
- 集合运算:
csk = {'dhoni': 101, 'jadeja': 102} india = {'virat': 103, 'jadeja': 102} csk1 = set(csk.keys()) india1 = set(india.keys()) # 两队共有球员 print(csk1 & india1) # 输出:{'jadeja'} # 任意一队有的球员 print(csk1 ^ india1) # 输出:{'virat', 'dhoni'} # 所有球员 print(csk1 | india1) # 输出:{'jadeja', 'dhoni', 'virat'}
登录后复制
- 单词频率统计:
sentence = "a rose is a rose is a rose" words = sentence.split() freq = {} for word in words: freq[word] = freq.get(word, 0) + 1 print(freq) # 输出:{'a': 3, 'rose': 3, 'is': 3}
登录后复制
- 计算分数:
players = {'jaiswal': 75, 'rohit': 55, 'virat': 95} total = sum(players.values()) average = total / len(players) high_score = max(players.values()) print("总分:", total) # 输出:总分: 225 print("平均分:", average) # 输出:平均分: 75.0 print("最高分:", high_score) # 输出:最高分: 95
登录后复制
所有代码都经过测试,确保可以正常运行并产生预期的输出。 代码中的注释也更加清晰,方便理解。
以上就是Python Day-Dictionary-练习、任务的详细内容,更多请关注php中文网其它相关文章!