【问题标题】:Complex iteration over PyTablesPyTables 上的复杂迭代
【发布时间】:2013-12-13 10:57:27
【问题描述】:

如果有人喜欢处理 Pytables,也许可以给我一个关于这个不起作用的复杂表达式的线索:

hdf5file = openFile("savedTable.h5", mode = 'r')
tab = hdf5file.getNode("/Data")

for i in xrange(1,10):
            result = [result + 1 for x in tab.where("""(col1== 1) & (col2 == 1) & (col3== i) & ((col4 == 1) | (col5 == 1) | (col6 == 1) | (col7== 1))""")]

Spyder 给我的只是这个典型的消息“无效语法”

特别注意循环 "for i in ...." 和查询 "... & (col3==i)" 我没有知道这部分是否可以这样完成。

【问题讨论】:

  • 你的意思是result = [x + 1 for x ...而不是result = [result + 1 for x ...
  • 我不知道,可能是的,如果找到查询中的条件,我想要的是将“结果”增加 1。由于我没有给 x 任何值,我认为不可能做 x + 1 之类的事情,但我会尝试。谢谢
  • 在这种情况下,请尝试result += sum(1 for _ in ...)result += len(tab.where(...))
  • 你确定吗?我不认为这种表迭代器 tab.where 可以与 len() 方法一起使用.....但无论如何我都可以尝试
  • 你能发布完整的回溯吗?

标签: python pytables


【解决方案1】:

你是对的,你不能这样做:

for i in xrange(1,10):
    tab.where('col3 == i')

请尝试:

for i in xrange(1,10):
    cond = 'col3 == %d' % i
    tab.where(cond)

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2018-08-19
    • 1970-01-01
    • 2021-03-06
    • 2014-12-02
    相关资源
    最近更新 更多