【发布时间】:2015-06-05 16:04:19
【问题描述】:
我需要一个数组来保存值,但我还想在以后随时编辑数组中的一些值。
我创建了一个包含一些随机值的数组并将其保存到磁盘。我可以阅读它。我想更新它,一个值为“23”的数组切片。当我再次阅读时,它看起来并没有改变。
如何更新这些值?
import numpy as np
import h5py
x, y = 100,20
# create
a = np.random.random(size=(x, y))
h5f = h5py.File('data.h5', 'w')
h5f.create_dataset('dataset_1', data=a)
print a[1][0:5] # [ 0.77474947 0.3618912 0.16000164 0.47827977 0.93955235]
h5f.close()
# read
h5f = h5py.File('data.h5','r')
b = h5f['dataset_1'][:]
print b[1][0:5] #[ 0.77474947 0.3618912 0.16000164 0.47827977 0.93955235]
h5f.close()
# update
h5f = h5py.File('data.h5', 'r+')
b = h5f['dataset_1'][:]
b[1][0:5] = 23
print b[1][0:5] #[ 23. 23. 23. 23. 23.]
h5f.close()
# read again
h5f = h5py.File('data.h5','r')
b = h5f['dataset_1'][:]
print b[1][0:5] #[ 0.77474947 0.3618912 0.16000164 0.47827977 0.93955235]
h5f.close()
【问题讨论】: