字典、列表之间相互嵌套

列表中嵌套字典

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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    字典、列表之间相互嵌套

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/307613

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午5:22
下一篇 2023年2月16日 上午5:26

相关推荐