patt = re.compile("\w+")
def makeIndex(filename):
index = {}
with open(filename) as handle:
for i, ln in enumerate(handle):
for word in patt.findall(ln):
index.setdefault(word, []).append(i+1)
return index
def printIndex(index):
for word, lst in index.items():
print "%s: %s" % (
word, ", ".join(map(str, lst))
)
def indexQuery(index, *args):
found = None
for word in args:
got = index.get(word, [])
if not got:
return None
if not found:
found = set(got)
else:
found &= set(got)
if not found:
return None
return list(found)
index = makeIndex("baiduqa.py")
printIndex(index)
print indexQuery(index, "in", "enumerate")
主键是表中的一个或多个字段,它的值用于惟一地标识表中的某一条记录.且不能为空;索引是对数据库表中一列或多列的值进行排序的一种结构,只有当经常查询索引列中的数据时,才需要在表上创建索引,使用索引可快速访问数据库表中的特定信息。
索引占用磁盘空间,并且降低添加、删除和更新行的速度。当然索引也有好处就是查询速度快,它利还是大于弊的所以请慎重使用索引。