多年后我仍然有同样的问题,但是借助索引和查询的力量,这个问题只是稍微痛苦,具体取决于表的大小。使用 readWhere 或 getListWhere 我认为问题大约是 O(n)
这就是我所做的......
1. 我创建了一个有两个索引的表。你可以在 PyTables 中使用多个索引:
http://pytables.github.com/usersguide/optimization.html#indexed-searches
一旦你的表是indexed,我也使用 LZO 压缩,你可以执行以下操作:
import tables
h5f = tables.openFile('filename.h5')
tbl = h5f.getNode('/data','data_table') # assumes group data and table data_table
counter += 0
for row in tbl:
ts = row['date'] # timestamp (ts) or date
uid = row['userID']
query = '(date == %d) & (userID == "%s")' % (ts, uid)
result = tbl.readWhere(query)
if len(result) > 1:
# Do something here
pass
counter += 1
if counter % 1000 == 0: print '%d rows processed'
现在我在这里编写的代码实际上有点慢。我确信那里有一些 PyTables 大师可以给你一个更好的答案。但这是我对性能的看法:
如果您知道您从干净的数据开始,即(无重复),那么您所要做的就是查询表一次以查找您有兴趣查找的键,这意味着您只需要做:
ts = row['date'] # timestamp (ts) or date
uid = row['userID']
query = '(date == %d) & (userID == "%s")' % (ts, uid)
result = tbl.getListWhere(query)
if len(result) == 0:
# key pair is not in table
# do what you were going to do
pass
elif len(result) > 1:
# Do something here, like get a handle to the row and update instead of append.
pass
如果您有大量时间检查重复项,请创建一个后台进程,该进程会在您的文件所在目录上爬网并搜索重复项。
我希望这对其他人有所帮助。