• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一泽涟漪
时光荏苒 白驹过隙
博客园    首页    新随笔    联系   管理    订阅  订阅
字典、列表之间相互嵌套

列表中嵌套字典

employee_1 = {'name': 'david', 'dept': 'ops', 'post': 'NOC', 'salary': 12000, 'id': 113}
employee_2 = {'name': 'brain', 'dept': 'auto', 'post': 'DBA', 'salary': 13000, 'id': 115}
employee_3 = {'name': 'chris', 'dept': 'search', 'post': 'PJM', 'salary': 20000, 'id': 150}
employees = [employee_1, employee_2, employee_3]

for employee in employees:
    employee['salary'] = employee['salary'] + 1000
    print(employee['salary'])

输出

13000
14000
21000
View Code

字典中嵌套列表

favorite_thing = {'foods': 'cheese', 'drinks': ['milk', 'juice', 'whisky']}
for drink in favorite_thing['drinks']:
    print("I love " + drink)

输出

I love milk
I love juice
I love whisky
View Code

字典嵌套列表

favorite_language = {'brain': ['python'], 'chris': ['c', 'ruby'], 'david': ['power shell'], 'tina': ['shell', 'go', 'c++']}

for name, languages in favorite_language.items():
    print("\n" + name.title() + " like programing language: ")
    for language in languages:
        print(language.title())

输出

Brain like programing language: 
Python

Chris like programing language: 
C
Ruby

David like programing language: 
Power Shell

Tina like programing language: 
Shell
Go
C++
View Code

 

 

 

===================来自一泽涟漪的博客,转载请标明出处 www.cnblogs.com/ilifeilong===================
posted on 2019-12-13 00:10  一泽涟漪  阅读(4560)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3