首页 > 其他 > 详细

字典、列表之间相互嵌套

时间:2019-12-13 00:25:05      阅读:123      评论:0      收藏:0      [点我收藏+]

列表中嵌套字典

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

 

 

 

字典、列表之间相互嵌套

原文:https://www.cnblogs.com/ilifeilong/p/12032627.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!