列表中 () 的含义是什么,例如。 [()] 在 Python 中?
What is the meaning of () inside a list eg. [()] in Python?
我遇到了一个 h5py 教程,其中访问 hdf5 文件的特定索引如下:
f = h5py.File('random.hdf5', 'r')
data = f['default'][()]
f.close()
print(data[10])
这样,即使文件关闭,数据仍然可以访问。似乎添加 [()]
不再使 data
成为一个简单的指针,而是数据对象本身。 [()]
是什么意思?
()
是空 tuple。 HDF5 数据集可以有任意数量的维度并支持索引,但一些数据集是零维的(它们存储单个标量值)。对于这些,h5py 使用带有空元组 [()]
的索引来访问该值。您不能使用 [0]
甚至 [:]
,因为这意味着至少要切分一个维度。
()
是一个空元组,使用空元组进行索引记录在 h5py's documentation:
An empty dataset has shape defined as None, which is the best way of determining whether > a dataset is empty or not. An empty dataset can be “read” in a similar way to scalar > datasets, i.e. if empty_dataset is an empty dataset,:
>>> empty_dataset[()]
h5py.Empty(dtype="f")
The dtype of the dataset can be accessed via .dtype as per normal. As empty > datasets cannot be sliced, some methods of datasets such as read_direct will raise an exception if used on a empty dataset.
我遇到了一个 h5py 教程,其中访问 hdf5 文件的特定索引如下:
f = h5py.File('random.hdf5', 'r')
data = f['default'][()]
f.close()
print(data[10])
这样,即使文件关闭,数据仍然可以访问。似乎添加 [()]
不再使 data
成为一个简单的指针,而是数据对象本身。 [()]
是什么意思?
()
是空 tuple。 HDF5 数据集可以有任意数量的维度并支持索引,但一些数据集是零维的(它们存储单个标量值)。对于这些,h5py 使用带有空元组 [()]
的索引来访问该值。您不能使用 [0]
甚至 [:]
,因为这意味着至少要切分一个维度。
()
是一个空元组,使用空元组进行索引记录在 h5py's documentation:
An empty dataset has shape defined as None, which is the best way of determining whether > a dataset is empty or not. An empty dataset can be “read” in a similar way to scalar > datasets, i.e. if empty_dataset is an empty dataset,:
>>> empty_dataset[()] h5py.Empty(dtype="f")
The dtype of the dataset can be accessed via .dtype as per normal. As empty > datasets cannot be sliced, some methods of datasets such as read_direct will raise an exception if used on a empty dataset.