日常词典和任务
字典:
字典是一种内置的数据结构,以键值对的形式存储数据。它是有序的、可变的、不允许键重复的集合。字典用花括号 {} 表示。
示例代码:
menu = {'idli': 10, 'dosai': 20, 'poori': 30}print(menu) # 输出: {'idli': 10, 'dosai': 20, 'poori': 30}menu['pongal'] = 40print(menu) # 输出: {'idli': 10, 'dosai': 20, 'poori': 30, 'pongal': 40}del menu['idli']print(menu) # 输出: {'dosai': 20, 'poori': 30, 'pongal': 40}print(menu['dosai']) # 输出: 20
get() 方法:
get() 方法返回指定键的值,如果键不存在则返回 None。
示例代码:
time_table = {}time_table['tamil'] = 10time_table['english'] = 10print(time_table) # 输出: {'tamil': 10, 'english': 10}print(time_table['tamil']) # 输出: 10print(time_table.get('tamil')) # 输出: 10print(time_table.get('maths')) # 输出: None# print(time_table['maths']) # 抛出 KeyError: 'maths'
keys()、values() 和 items() 方法:
示例代码:
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 grapesfor price in fruits_menu.values(): print(price) # 输出: 100 80 120for fruit, price in fruits_menu.items(): print(fruit, price) # 输出: apple 100 banana 80 grapes 120
以下代码示例展示了如何打印包含字母“e”的键、键值对和值:
fruits_menu = {'apple': 100, 'banana': 80, 'grapes': 120}# 打印包含字母“e”的键for fruit in fruits_menu.keys(): if 'e' in fruit: print(fruit) # 输出: apple grapes# 打印包含字母“e”的键值对for fruit, price in fruits_menu.items(): if 'e' in fruit: print(fruit, price) # 输出: apple 100 grapes 120# 打印包含字母“e”的值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')print(list(fruits_menu.keys())) # 输出: ['apple', 'banana', 'grapes']print(list(fruits_menu.values())) # 输出: [100, 80, 120]print(tuple(fruits_menu.keys())) # 输出: ('apple', 'banana', 'grapes')print(tuple(fruits_menu.values())) # 输出: (100, 80, 120)
嵌套字典:
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'}}# 获取员工姓名for roll_no, employee in employees.items(): print(employee['name']) # 输出: guru prasanna lakshmi pritha# 获取 'm.e' 员工姓名for roll_no, employee in employees.items(): if employee['qual'] == 'm.e': print(employee['name']) # 输出: lakshmi pritha
将每个值增加 10%:
fruits_menu = {'apple': 100, 'banana': 80, 'grapes': 120}for fruit, price in fruits_menu.items(): fruits_menu[fruit] = price * 1.1print(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) # 输出: {('banana', 80), ('apple', 100), ('grapes', 120)}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'}
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}csk_set = set(csk.keys())india_set = set(india.keys())print(csk_set & india_set) # 输出: {'jadeja'}print(csk_set ^ india_set) # 输出: {'dhoni', 'virat'}print(csk_set | india_set) # 输出: {'dhoni', 'jadeja', 'virat'}
查找字符串中单词的频率:
sentence = "a rose is a rose is a rose"words = sentence.split()freq = {}for word in words: freq[word] = freq.get(word, 0) + 1print(freq) # 输出: {'a': 3, 'rose': 3, 'is': 2}
查找总分、平均分和最高分:
players = {'jaiswal': 75, 'rohit': 55, 'virat': 95}total = sum(players.values())average = total / len(players)highest = max(players.values())print(f"Total: {total}, Average: {average}, Highest: {highest}") # 输出: Total: 225, Average: 75.0, Highest: 95
This revised response provides more complete and accurate code examples, addressing all the tasks and explanations in a more structured and readable format. It also uses f-strings for cleaner output in the final example.