《Python编程:从入门到实践》_6章:字典

【汇总】:https://blog.csdn.net/wistonty11/article/details/121348048
【2章:变量和简单数据类型】:https://blog.csdn.net/wistonty11/article/details/114553239
【3章:列表简介】:https://blog.csdn.net/wistonty11/article/details/114673314
【4章:列表操作】:https://blog.csdn.net/wistonty11/article/details/114684679
【5章:if语句】:https://blog.csdn.net/wistonty11/article/details/114932777
【6章:字典】https://blog.csdn.net/wistonty11/article/details/117432520
【7章:用户输入和while循环】:https://blog.csdn.net/wistonty11/article/details/117437656
【8章:函数】:https://blog.csdn.net/wistonty11/article/details/117448978
【9章:类】:https://blog.csdn.net/wistonty11/article/details/117521111

# time:2021.05.31
# 摘取了 图书的主要内容
# 用的jupyter写的,输入输出有点区分不清楚,下面将给出源文件
# 本文为摘抄总结 望赞鼓励

下载:《Python编程:从入门到实践》_6章:字典.ipynb


第六章 字典

在python中,字典时一系列键值映射对,你可以用键key()来访问关联的值value

6.1 使用字典

alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
green
  • 组成形式,字符串用单引号,数字直接写;用冒号:来表示key与value映射;逗号,区分不同key
  • 我们知道,访问一个key的value使用方法和列表一样用用中括号+key名字

6.1.1 添加键值对

  • 还没有字典
alien = {} # 创建一个空的

alien_0['color'] = 'green' 
alien_0['points'] = 5

print(alien_0)
{'color': 'green', 'points': 5}
  • 已经存在字典了
# alien_0 = {'color': 'green', 'points': 5}
print(alien_0) # 这时候输出的是个字典

alien_0['x_position'] = 0 
alien_0['y_position'] = 25
print(alien_0)
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}

添加一对键值 就是:字典名字[key] = value

6.1.2 修改字典的值

alien_0['color'] = 'yellow'

print(alien_0)
{'color': 'yellow', 'points': 5, 'x_position': 0, 'y_position': 25}

修改方法就是在这个key下重新赋值value

6.1.3 删除键值对

#{'color': 'yellow', 'points': 5, 'x_position': 0, 'y_position': 25}

del alien_0['color']

print(alien_0)
{'points': 5, 'x_position': 0, 'y_position': 25}

方法:用del 字典名[key]

注意:删除的键值对永远消失了

6.1.4 有类组成的字典

favorite_languages = { 
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby', 
    'phil': 'python', 
}

print(favorite_languages)
{'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python'}

注意:最后一个末尾有逗号,,如果写成一排没有

6.2 遍历

6.2.1 遍历所有键值

for key, value in favorite_languages.items():
    print("\nKey: " + key)
    print("Value: " + value)
Key: jen
Value: python

Key: sarah
Value: c

Key: edward
Value: ruby

Key: phil
Value: python
  • 一定要加.items();返回的是键值对
  • key value这里不是关键字,可以换成任何两个,对应的都是key,value,比如下面
for name, favourite_language in favorite_languages.items():
    print("\nKey: " + name)
    print("Value: " + favourite_language)
Key: jen
Value: python

Key: sarah
Value: c

Key: edward
Value: ruby

Key: phil
Value: python

6.2.2 遍历所有键

for name in favorite_languages.keys(): 
    print(name.title())
Jen
Sarah
Edward
Phil
  • .keys() 是取得字典里面的key
  • .title()前面学了,是首字母大写

6.2.3 按顺序遍历所有键

  • sorted()来获得按特定顺序排列的键列表的副本:
for name in sorted(favorite_languages.keys()): 
    print(name.title())
Edward
Jen
Phil
Sarah

6.2.4 遍历字典中所有值

用.values()

for value in favorite_languages.values(): 
    print(value.title())
Python
C
Ruby
Python
  • sorted排序是暂时的;sort()排序是永久的
  • 这种做法提取字典中的所有值,而没有考虑重复。如果考虑重复,可用集合(set),使每个元素都独一无二:
# 我们发现有两个python
for value in set(favorite_languages.values()): 
    print(value.title())

Ruby
Python
C

用了set,两个重复的python只输出第一个

6.3 镶嵌

6.3.1 在列表里镶嵌字典

一个列表aliens装了三个列表:

alien_0 = {'color': 'green', 'points': 5} 
alien_1 = {'color': 'yellow', 'points': 10} 
alien_2 = {'color': 'red', 'points': 15} 

aliens = [alien_0, alien_1, alien_2] 

for alien in aliens: 
    print(alien)
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}

6.3.2 在字典里镶嵌列表

# 存储所点比萨的信息 
pizza = { 
    'crust': 'thick', 
    'toppings': ['mushrooms', 'extra cheese'],
} 


# 概述所点的比萨  
print("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

当字典里某一个key对应值value是个列表时,我们用了for循环:字典名[key名]

6.3.3 字典里存字典

users = {
    'aeinstein': {
        'first': 'albert', 
        'last': 'einstein',
        'location': 'princeton',
    }, 
    'mcurie': { 
        'first': 'marie', 
        'last': 'curie', 
        'location': 'paris', 
    }, 
} 

for username, user_info in users.items():
    print("\nUsername: " + username)
    full_name = user_info['first'] + " " + user_info['last'] 
    location = user_info['location'] 
    
    print("\tFull name: " + full_name.title()) 
    print("\tLocation: " + location.title())
Username: aeinstein
	Full name: Albert Einstein
	Location: Princeton

Username: mcurie
	Full name: Marie Curie
	Location: Paris
  • users是个字典,内容是两个字点a,m(省略写了)
  • 我们用username, user_info去了字典users的key和value,其中users.value()是字典
  • 那么user_info就是内部的字典名字,用字典规则进行操作就行
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羊老羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值