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 |

相关推荐
小徐不徐说5 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang5 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
王中阳Go5 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
Wang's Blog5 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象3276 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
weixin_307779136 小时前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展
蒸蒸yyyyzwd6 小时前
AI应用相关可能的面试问题
笔记
Dxy12393102167 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷7 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
云和数据.ChenGuang7 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain