您很可能在这项工作中使用了错误的工具。如果您想保留大量索引数据,我强烈建议您使用带有 ORM(如SQLObject或SQL Alchemy )的 SQLite 磁盘数据库(或者,当然,只是普通数据库) 。
这些将处理诸如兼容性、优化格式以及不同时将所有数据保存在内存中等平凡的事情,以免内存不足......
补充:因为无论如何我都在做几乎相同的事情,但主要是因为我是一个很好的人,这里有一个演示似乎可以满足您的需求(它将在您当前的目录中创建一个 SQLite 文件,然后将其删除如果同名的文件已经存在,那么先把它放在空的地方):
import sqlobject
from sqlobject import SQLObject, UnicodeCol, ForeignKey, IntCol, SQLMultipleJoin
import os
DB_NAME = "mydb"
ENCODING = "utf8"
class Document(SQLObject):
dbName = UnicodeCol(dbEncoding=ENCODING)
class Location(SQLObject):
""" Location of each individual occurrence of a word within a document.
"""
dbWord = UnicodeCol(dbEncoding=ENCODING)
dbDocument = ForeignKey('Document')
dbLocation = IntCol()
TEST_DATA = {
'one' : {
'doc1' : [1,2,10],
'doc3' : [6],
},
'two' : {
'doc1' : [2, 13],
'doc2' : [5,6,7],
},
'three' : {
'doc3' : [1],
},
}
if __name__ == "__main__":
db_filename = os.path.abspath(DB_NAME)
if os.path.exists(db_filename):
os.unlink(db_filename)
connection = sqlobject.connectionForURI("sqlite:%s" % (db_filename))
sqlobject.sqlhub.processConnection = connection
# Create the tables
Document.createTable()
Location.createTable()
# Import the dict data:
for word, locs in TEST_DATA.items():
for doc, indices in locs.items():
sql_doc = Document(dbName=doc)
for index in indices:
Location(dbWord=word, dbDocument=sql_doc, dbLocation=index)
# Let's check out the data... where can we find 'two'?
locs_for_two = Location.selectBy(dbWord = 'two')
# Or...
# locs_for_two = Location.select(Location.q.dbWord == 'two')
print "Word 'two' found at..."
for loc in locs_for_two:
print "Found: %s, p%s" % (loc.dbDocument.dbName, loc.dbLocation)
# What documents have 'one' in them?
docs_with_one = Location.selectBy(dbWord = 'one').throughTo.dbDocument
print
print "Word 'one' found in documents..."
for doc in docs_with_one:
print "Found: %s" % doc.dbName
这当然不是这样做的唯一方法(或不一定是最好的方法)。Document 或 Word 表是否应与 Location 表分开,取决于您的数据和典型用法。在您的情况下,“Word”表可能是一个单独的表,其中添加了一些索引和唯一性设置。