gpt4 book ai didi

python-2.7 - 在 python3 中使用 h5py 发现 key

转载 作者:行者123 更新时间:2023-12-03 13:44:56 25 4
gpt4 key购买 nike

python2.7 ,我可以分析一个hdf5文件 key 使用

$ python
>>> import h5py
>>> f = h5py.File('example.h5', 'r')
>>> f.keys()
[u'some_key']

然而,在 python3.4 ,我得到不同的东西:
$ python3 -q
>>> import h5py
>>> f = h5py.File('example.h5', 'r')
>>> f.keys()
KeysViewWithLock(<HDF5 file "example.h5" (mode r)>)

什么是 KeysViewWithLock ,以及如何在 Python3 中检查我的 HDF5 key ?

最佳答案

来自 h5py 的网站 (http://docs.h5py.org/en/latest/high/group.html#dict-interface-and-links):

When using h5py from Python 3, the keys(), values() and items() methods will return view-like objects instead of lists. These objects support containership testing and iteration, but can’t be sliced like lists.



这解释了为什么我们无法查看它们。最简单的答案是将它们转换为列表:
>>> list(for.keys())

不幸的是,我在 iPython 中运行,它使用命令“l”。这意味着该方法行不通。

为了实际查看它们,我们需要利用容器测试和迭代。容器船测试意味着我们必须已经知道 key ,所以它已经过时了。幸运的是,使用迭代很简单:
>>> [key for key in f.keys()]
['mins', 'rects_x', 'rects_y']

我创建了一个自动执行此操作的简单函数:
def keys(f):
return [key for key in f.keys()]

然后你得到:
>>> keys(f)
['mins', 'rects_x', 'rects_y']

关于python-2.7 - 在 python3 中使用 h5py 发现 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31037088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com