python笔记基础--字典(3)

目录

1.字典使用

1.1由类似对象组成的字典

[1.2用get( )访问值](#1.2用get( )访问值)

2.遍历字典

2.1遍历所有键值对items()

2.2遍历字典中键key()

2.3遍历字典中值value()

3.嵌套

3.1字典列表

3.2在字典中存储列表

3.3在字典中存储字典


字典:一系列键值对;可存储的信息量几乎不受限制。

1.字典使用

|----------|----------------------------------------------------------|--------------------------------------------------------------------|
| alien_0 = { 'color':'green' , 'points':5 } || color是green,points是5 |
| 访问字典中的值 | print(alien_0['color']) | green |
| 访问字典中的值 | print(alien_0['points']) | 5 |
| 访问字典中的值 | new_points = alien_0['points'] | |
| 添加键值对 | alien_0['x_position'] = 0 alien_0['y_position'] = 25 | {'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25} |
| 创建空字典 | alien_0 = { } | {} |
| 修改字典值 | alien_0['color'] = 'yellow' | {'color': 'yellow', 'points': 5} |
| 删除键值对del | del alien_0['color'] | {'points': 5} |

1.1由类似对象组成的字典

|-----------------------------------------------------------------------------------------|--------------------------------|
| languages = { 'jen':'Python', 'ann':'C', 'sarah':'Java', } ||
| 代码 | 结果 |
| language = languages['ann'].title() print(f"Ann's favourite language is {language}.") | Ann's favourite language is C. |

1.2用get( )访问值

|--------------------------------------------------------------------|-----------|
| alien_0={'color':'yellow', 'speed':5} ||
| 代码 | 结果 |
| print_value = alien_0.get('points','No points') print(print_value) | No points |
| print_value = alien_0.get('color','No color') print(print_value) | yellow |

2.遍历字典

2.1遍历所有键值对items()

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------|
| user_0 = {'username':'ann' , 'first':'enrico'} ||
| 代码 | 结果 |
| for key, value in user_0****.items()**** : print(f"\nkey:{key}") print(f"Value:{value}") | key:username Value:ann key:first Value:enrico |
| languages = { 'jen':'Python', 'ann':'C', 'sarah':'Java', } for name, language in languages****.items()**** : print(f"{name.title()}'s favourite language is {language}.") | Jen's favourite language is Python. Ann's favourite language is C. Sarah's favourite language is Java. |

2.2遍历字典中键key()

|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------|
| language_s = { 'jen':'Python', 'ann':'C', 'sarah':'Java'} |||
| | 代码 | 结果 |
| 遍历所有key值 | for name in language_s.keys(): print(name.title()) | Jen Ann Sarah |
| 遍历部分key值 | friends = ['ann','dylan'] for name in language_s.keys(): if name in friends: language = language_s[name].title() print(f"{name.title()},you love {language}.") | Ann,you love C. |
| 查看某个key 是否满足 | if 'ann' not in language_s: print("Ann,please take our poll!") else: print("Ann.") | Ann. |
| 按特定顺序遍历 | for name in sorted(language_s.keys()): print(f"{name.title()}") | Ann Jen Sarah |

2.3遍历字典中值value()

|-------------|------------------------------------------------------------------------|----------------------|
| language_s = { 'jen':'Python', 'ann':'C', 'sarah':'Java'} |||
| | 代码 | 结果 |
| 遍历所有 value值 | for language in language_s.values(): print(f"{language.title()}") | Python C Java Python |
| 剔除重复项set() | for language in set(language_s.values()): print(f"{language.title()}") | C Java Python |

3.嵌套

嵌套:将一些列字典存储在列表中,或将列表作为值存储在字典中。

3.1字典列表

|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| | 代码 | 结果 |
| 多个字典存储在一个列表中 | alien_0 = {'color':'yellow','speed':5} alien_1 = {'color':'blue','speed':8} alien_2 = {'color':'green','speed':7} aliens = [alien_0,alien_1,alien_2] for alien in aliens: print(alien) | {'color': 'yellow', 'speed': 5} {'color': 'blue', 'speed': 8} {'color': 'green', 'speed': 7} |
| 生成range() | aliens=[] #创建30个蓝色外星人 for alien_number in range(30): new_aliens = {'color':'blue','points':5,'speed':'slow'} aliens.append(new_aliens) #显示前5个外星人 for alien in aliens[:5]: print(alien) print("......") #显示创建了多少个外星人 print(f"Total number of aliens:{len(aliens)}") | {'color': 'blue', 'points': 5, 'speed': 'slow'} {'color': 'blue', 'points': 5, 'speed': 'slow'} {'color': 'blue', 'points': 5, 'speed': 'slow'} ...... Total number of aliens:30 |
| 修改前三个外星人 | #修改前三个外星人的值 for alien in aliens[:3]: if alien['color'] == 'blue': alien['color'] = 'yellow' alien['points'] = 7 alien['speed'] = 'medium' #显示前5个外星人 for alien in aliens[:5]: print(alien) print("......") | {'color': 'yellow', 'points': 7, 'speed': 'medium'} {'color': 'yellow', 'points': 7, 'speed': 'medium'} {'color': 'yellow', 'points': 7, 'speed': 'medium'} {'color': 'blue', 'points': 5, 'speed': 'slow'} {'color': 'blue', 'points': 5, 'speed': 'slow'} ...... |
| 修改前三个外星人 | #修改前三个外星人的值 for alien in aliens[:3]: if alien['color'] == 'blue': alien['color'] = 'yellow' alien['points'] = 7 alien['speed'] = 'medium' elif alien['color'] == 'yellow': alien['color'] = 'red' alien['points'] = 13 alien['speed'] = 'fast' #显示前5个外星人 for alien in aliens[:5]: print(alien) print("......") | {'color': 'yellow', 'points': 7, 'speed': 'medium'} {'color': 'yellow', 'points': 7, 'speed': 'medium'} {'color': 'yellow', 'points': 7, 'speed': 'medium'} {'color': 'blue', 'points': 5, 'speed': 'slow'} {'color': 'blue', 'points': 5, 'speed': 'slow'} ...... |

3.2在字典中存储列表

将列表存储在字典中。

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| 代码 | 结果 |
| #存储所点披萨的信息 pizza = { 'crust':'thick', 'toppings':['mushrooms','extra cheese'], } #概述所有的披萨 print(f"You ordered a {pizza['crust']}-crust pizza " "with the following toppings") for topping in pizza['toppings']: print("\t" + topping) | You ordered a ++++thick++++-crust pizza with the following toppings ++++mushrooms++++ ++++extra cheese++++ |
| language_s = { 'jen':['Python'], 'ann':['C','python']} for name, languages in language_s.items(): print(f"{name.title()}'s languages are:") for language in languages: print(f"\t{language.title()}") | Jen's languages are: Python Ann's languages are: C Python |

3.3在字典中存储字典

在字典中嵌套字典。

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|
| 代码 | 结果 |
| users = { '开心果':{ 'first':'albert', 'last':'einstein', 'location':'princeton' }, '紫罗兰':{ 'first':'marie', 'last':'curie', 'location':'paris' } } for username, user_info in users.items(): print(f"\nUsername:{username}") full_name = f"{user_info['first']} {user_info['last']}" location = user_info['location'] print(f"Full name:{full_name}") print(f"Location:{location}") | Username:开心果 Full name:albert einstein Location:princeton Username:紫罗兰 Full name:marie curie Location:paris |

相关推荐
じ☆冷颜〃3 小时前
分布式系统中网络技术的演进与异构融合架构(HFNA)
笔记·python·物联网·设计模式·架构·云计算
夜思红尘6 小时前
算法--双指针
python·算法·剪枝
人工智能训练6 小时前
OpenEnler等Linux系统中安装git工具的方法
linux·运维·服务器·git·vscode·python·ubuntu
散峰而望6 小时前
【算法竞赛】C++函数详解:从定义、调用到高级用法
c语言·开发语言·数据结构·c++·算法·github
冷凝雨6 小时前
复数乘法(C & Simulink)
c语言·开发语言·信号处理·simulink·dsp
CoderCodingNo6 小时前
【GESP】C++五级真题(贪心思想考点) luogu-B4071 [GESP202412 五级] 武器强化
开发语言·c++·算法
郭涤生6 小时前
第十章_信号_《UNIX环境高级编程(第三版)》_笔记
服务器·笔记·unix
0和1的舞者6 小时前
Spring AOP详解(一)
java·开发语言·前端·spring·aop·面向切面
MoonBit月兔6 小时前
年终 Meetup:走进腾讯|AI 原生编程与 Code Agent 实战交流会
大数据·开发语言·人工智能·腾讯云·moonbit
QT 小鲜肉6 小时前
【Linux命令大全】001.文件管理之which命令(实操篇)
linux·运维·服务器·前端·chrome·笔记