字典、列表之间相互嵌套

僤鯓⒐⒋嵵緔 提交于 2019-12-13 00:47:28

列表中嵌套字典

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

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!