列表中嵌套字典
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
字典中嵌套列表
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
字典嵌套列表
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++
来源:https://www.cnblogs.com/ilifeilong/p/12032627.html