我有这样的字典:
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'
任何线索如何将嵌套字典中的项目从数组更改为列表?
答案 0 :(得分:1)
您应该执行以下操作:
for key1, value1 in workersDays.items():
for key2, value2 in value1.items():
do_this()
do_that()
通过这种方式,您认为value1
确实是dict
,并在其自己的key
和value
上进行迭代。
答案 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)