python .npy 存取 dict
import numpy as np dict = {'a' : {1,2,3}, 'b': {4,5,6}} np.save('dict.npy', dict) dict_load=np.load('dict.npy', allow_pickle=True) print("dict =", dict_load.item()) print("dict['a'] =", dict_load.item()['a'])
读取的时候如果不用dict_load.item()['a'],而是直接用dict_load['a'],会报如下错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices