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_sname.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 |

相关推荐
老歌老听老掉牙1 小时前
空间曲线数据的拼接与几何变换分析
python·曲线
Cc.Y1 小时前
Java零基础入门:字符串深度掌握——从基础API到StringBuilder性能优化
java·开发语言·性能优化
databook5 小时前
使用 SciPy 库进行时间序列分析
python·数据分析·scipy
java资料站5 小时前
十一、评估测试
开发语言·人工智能·python
szial5 小时前
网络爬虫与 CDP 实战(二):浏览器能访问,Python 却被拒绝?拆开登录态与 CSRF
爬虫·python·csrf
左手の明天6 小时前
SLIP 协议封装与解析详解:原理、C 代码与实战案例
linux·c语言·开发语言·c++
海上小飞龙6 小时前
【KMP算法-下篇】同一道题:Java 库函数 2600 微秒,手写 KMP 16 微秒
java·开发语言·算法
青少儿编程课堂6 小时前
后缀自动机解析:本质不同子串与最长重复片段统计
c++·python·算法·bfs·信息学竞赛
朝朝辞暮i6 小时前
C++ 第 12 课:局部变量、作用域、变量生命周期
开发语言·c++·算法
狗凯之家源码网6 小时前
淘客口令落地页生成器 PHP 版深度评测
开发语言·php