我的新PyTables,并期待在使用它来处理来自基于代理的建模仿真生成并存储在数据HDF5。我与一个39 MB的测试文件的工作,和我遇到了一些陌生感。下面是表的布局:

    /example/agt_coords (Table(2000000,)) ''
  description := {
  "agent": Int32Col(shape=(), dflt=0, pos=0),
  "x": Float64Col(shape=(), dflt=0.0, pos=1),
  "y": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (20000,)

下面就是我如何在Python访问它:

from tables import *
>>> h5file = openFile("alternate_hose_test.h5", "a")

h5file.root.example.agt_coords
/example/agt_coords (Table(2000000,)) ''
  description := {
  "agent": Int32Col(shape=(), dflt=0, pos=0),
  "x": Float64Col(shape=(), dflt=0.0, pos=1),
  "y": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (20000,)
>>> coords = h5file.root.example.agt_coords

现在这里的事情变得怪异。

[x for x in coords[1:100] if x['agent'] == 1]
[(1, 25.0, 78.0), (1, 25.0, 78.0)]
>>> [x for x in coords if x['agent'] == 1]
[(1000000, 25.0, 78.0), (1000000, 25.0, 78.0)]
>>> [x for x in coords.iterrows() if x['agent'] == 1]
[(1000000, 25.0, 78.0), (1000000, 25.0, 78.0)]
>>> [x['agent'] for x in coords[1:100] if x['agent'] == 1]
[1, 1]
>>> [x['agent'] for x in coords if x['agent'] == 1]
[1, 1]

我不明白为什么值被搞砸了,当我遍历整个表,而不是当我把整个行集的一小部分。我敢肯定,这是我如何使用库是错误的,所以在这个问题上的任何帮助将非常感激。

有帮助吗?

解决方案

这是混乱的一个非常普遍的点遍历Table对象时,

当我们通过遍历Table项目你得到的类型不是在该项目中的数据,但在当前行的访问表。因此,与

[x for x in coords if x['agent'] == 1]

在创建行的访问者的列表,所有指向表,最后一行的“当前”行。但是,当你做

[x["agent"] for x in coords if x['agent'] == 1]

使用访问在构建列表。

该解决方案让所有为您打造的列表中,通过使用在每次迭代存取所需的数据。有两个选项

[x[:] for x in coords if x['agent'] == 1]

[x.fetch_all_fields() for x in coords if x['agent'] == 1]

前者构建元组的列表。后者返回一个NumPy的空隙对象。 IIRC,二是快,但前者可能更有意义,你的目的。

下面的一个很好的解释从该PyTables开发商。 在未来的版本中,打印一行访问对象可能不是简单显示数据,但状态,这是一个行存取对象

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top