python – PyTables问题 – 迭代表的子集时的结果不同
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – PyTables问题 – 迭代表的子集时的结果不同,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2103字,纯文字阅读大概需要4分钟。
内容图文
我是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对象时,这是一个非常常见的混淆点,
当您遍历表时,您获得的项目类型不是项目中的数据,而是当前行中表格的访问者.所以
[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 void对象. IIRC,第二个更快,但前者可能对你更有意义.
这是来自PyTables开发人员的a good explanation. In future releases, printing a row accessor object may not simply show the data, but state that it’s a row accessor object.
内容总结
以上是互联网集市为您收集整理的python – PyTables问题 – 迭代表的子集时的结果不同全部内容,希望文章能够帮你解决python – PyTables问题 – 迭代表的子集时的结果不同所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。