难以将嵌套字典的元素从数组更改为列表

时间:2019-04-19 06:44:32

标签: python dictionary

我有这样的字典:

workersDays=
{'worker1': {datetime.date(2017, 10, 20): array(['1', '3', '64', '56', '34', '23']),
               (datetime.date(2017, 10, 20);array(['12', '34', '56', '78', '98', '45']
worker2: { datetime.date(2017, 10, 21)......................

}

我试试这个:

import numpy as np
for worker in workersDays.keys():
        for day in worker:
             worker[day]=worker[day].tolist()
display(workersDays)

但是我唯一遇到的错误是:

KeyError: '1'

任何线索如何将嵌套字典中的项目从数组更改为列表?

2 个答案:

答案 0 :(得分:1)

您应该执行以下操作:

for key1, value1 in workersDays.items():
    for key2, value2 in value1.items():
        do_this()
        do_that()

通过这种方式,您认为value1确实是dict,并在其自己的keyvalue上进行迭代。

答案 1 :(得分:0)

尝试:

import numpy as np
for worker in workersDays.keys():
        for day in workersDays[worker].keys():
             workersDays[worker][day]=workersDays[worker][day].tolist()
display(workersDays)