如何在Python中保存大(不是很大)的分类? [英] How to save big (not huge) dictonaries in Python?
问题描述
我的字典将包含数千个键,每个键具有1000x1000 numpy数组作为值。我不需要该文件是人类可读的。小尺寸和快速加载时间更重要。
My dictionary will consist of several thousand keys which each key having a 1000x1000 numpy array as value. I don't need the file to be human readable. Small size and fast loading times are more important.
首先我试过 savemat ,但我遇到了问题。 Pickle 导致了一个巨大的文件。我认为csv是一样的。我已阅读推荐使用 json (可读文字大概)或db (假设复杂)。你会为我的案例推荐什么?
First I tried savemat, but I ran into problems. Pickle resulted in a huge file. I assume the same for csv. I've read posts recommending using json (readable text probably huge) or db (assumingly complicated). What would you recommend for my case?
推荐答案
如果你有一个字典,键是字符串,值是数组,这个:
If you have a dictionary where the keys are strings and the values are arrays, like this:
>>> import numpy
>>> arrs = {'a': numpy.array([1,2]),
'b': numpy.array([3,4]),
'c': numpy.array([5,6])}
您可以使用 numpy.savez 通过键将其保存到压缩文件中:
You can use numpy.savez to save them, by key, to a compressed file:
>>> numpy.savez('file.npz', **arrs)
要加载它: p>
To load it back:
>>> npzfile = numpy.load('file.npz')
>>> npzfile
<numpy.lib.npyio.NpzFile object at 0x1fa7610>
>>> npzfile['a']
array([1, 2])
>>> npzfile['b']
array([3, 4])
>>> npzfile['c']
array([5, 6])
这篇关于如何在Python中保存大(不是很大)的分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!