python字典应如何存储在pytables中? [英] How should python dictionaries be stored in pytables?
问题描述
pytables本身不支持python字典.我所采用的方法是制作以下形式的数据结构:
pytables doesn't natively support python dictionaries. The way I've approached it is to make a data structure of the form:
tables_dict = {
'key' : tables.StringCol(itemsize=40),
'value' : tables.Int32Col(),
}
(请注意,我确保键的长度小于40个字符),然后使用以下结构创建表:
(note that I ensure that the keys are <40 characters long) and then create a table using this structure:
file_handle.createTable('/', 'dictionary', tables_dict)
,然后用以下命令填充它:
and then populate it with:
file_handle.dictionary.append(dictionary.items())
并使用以下命令检索数据:
and retrieve data with:
dict(file_handle.dictionary.read())
这可以,但是重新读字典非常慢.我认为问题在于 read()
函数导致整个字典被加载到内存中,这实际上不是必须的.有更好的方法吗?
This works ok, but reading the dictionary back in is extremely slow. I think the problem is that the read()
function is causing the entire dictionary to be loaded into memory, which shouldn't really be necessary. Is there a better way to do this?
推荐答案
您可以要求PyTables在表内部进行搜索,还可以在键列上创建索引以加快速度.
You can ask PyTables to search inside the table, and also create an index on the key column to speed that up.
要创建索引:
table.cols.key.createIndex()
要查询 key
等于变量 search_key
的值:
[row['value'] for row in table.where('key == search_key')]
http://pytables.github.com/usersguide/optimization.html#searchoptim
这篇关于python字典应如何存储在pytables中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!