我希望获得预期的输出:Orea的工资为2344
但是我在访问第二本词典“ sales”时遇到问题。
Department_Employee_data= [{'Purchase': ['John', 'Avery', 'Obama'], 'Sales': ['Orea', 'Stark']},
{'Purchase': [2000, 4000, 10000], 'Sales': [2344, 32442]}]
for data in Department_Employee_data:
if data['Sales'][0] == 'Orea':
print(data['Sales'][0], " has a salary of ", data[[1]['Sales']])
请告知
答案 0 :(得分:0)
您的Department_Employee_data
是list
,其中包含您的词典。由于您已经对其进行了迭代,因此data
仅保留该迭代的当前结果-列表中的第一(第二,第三...)字典,因此您无法访问 next < / em>列表中的字典。
如果要遍历第一个字典中的Sales
键的值,然后访问下一个字典中相同索引的值,则必须使用enumerate
来获得匹配项索引,例如:
for index, data in enumerate(Department_Employee_data[0]["Sales"]):
if data == "Orea":
print("Orea has a salary of {}".format(Department_Employee_data[1]["Sales"][index]))
答案 1 :(得分:0)
我认为您的列表将始终把雇员放在第一位,将薪水放在第二位
employees,salaries=Department_Employee_data;
for department in employees:
for employee,salary in zip(employees[department],salaries[department]):
# do whatever you want with the employee, salary and department value
print("{} of department {} has a salary of {}".format(employee,department,salary))