前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Python基础】字典的嵌套

【Python基础】字典的嵌套

作者头像
DataScience
发布2020-01-01 22:37:05
2.7K0
发布2020-01-01 22:37:05
举报
文章被收录于专栏:A2DataA2Data

本文标识 : P00017

本文编辑 : 采药

编程工具 : Python

阅读时长 : 5分钟


嵌套

今天主要了解一下字典的嵌套。

很多时候,我们需要将一系列字典存储在列表中,或者将列表作为值存储在字典里,这就是嵌套。

字典列表

我们有一些关于描述卡片的信息,每张卡片都是有颜色和数字组成。

代码语言:javascript
复制
alien0={'color':'green','points':5}
alien1={'color':'yellow','points':10}
alien2={'color':'yellow','points':15}

aliens=[alien0,alien1,alien2]
for alien in aliens:
  print(alien)

输出结果:

代码语言:javascript
复制
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'yellow', 'points': 15}

首先我们定义了三个字典,每个字典代表一个卡牌,我们将三个字典存入一个列表中,这就是嵌套。

我们再用代码自动生成30个卡牌

代码语言:javascript
复制
aliens=[]
for alien_number in range(30):
    new_alien={'color':'green','points':5}
    aliens.append(new_alien)

for alien in aliens[:5]:#显示前5个卡牌
    print(alien)
print("······")
print("卡牌总数: "+str(len(aliens)))

输出结果:

代码语言:javascript
复制
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
······
卡牌总数: 30

但是这些卡牌都一模一样,现在我们需要将卡牌的前三个修改为黄色

代码语言:javascript
复制
aliens=[]
for alien_number in range(30):
    new_alien={'color':'green','points':5}
    aliens.append(new_alien)

for alien in aliens[:5]:#显示前5个卡牌
    print(alien)
print("······")
print("卡牌总数: "+str(len(aliens)))

for alien in aliens[0:3]:
    alien['color']='yellow'

#显示前5个卡牌
for alien in aliens[0:5]:
    print(alien)

结果:

代码语言:javascript
复制
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}
······
卡牌总数: 30
{'color': 'yellow', 'points': 5}
{'color': 'yellow', 'points': 5}
{'color': 'yellow', 'points': 5}
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5}

字典中存列表

有时候我们需要在字典中存储列表,比如每个人喜欢的编程语言可能有好几个。

代码语言:javascript
复制
fav_languages={
    'jen':['python','ruby'],
    'sarah':['c'],
    'edward':['ruby','go'],
    'phil':['python','haskell']
              }

for name,languages in  fav_languages.items():
    print("\n" + name.title() +"' favorite language are:")
    for language in languages :
        print("\t"+language.title())

输出结果:

代码语言:javascript
复制
Edward' favorite language are:
  Ruby
  Go

Jen' favorite language are:
  Python
  Ruby

Phil' favorite language are:
  Python
  Haskell

Sarah' favorite language are:
  C

字典中存字典

在字典中可以嵌套字典,但是这样代码就稍微复杂一点。比如你有很多网站用户,每个都有自己的用户名,可作为键,每个用户的信息存储在一个字典。

代码语言:javascript
复制
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())

输出结果:

代码语言:javascript
复制
Username: mcurie
  Full name: Marie Curie
  location: Paris

Username: aeinstein
  Full name: Albert Einstein
  location: Princeton

期待您的进步

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataScience 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 嵌套
  • 字典列表
  • 字典中存列表
  • 字典中存字典
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档