[英]Python data structure to mimic relational databases
I would like to store a lot of instances of some data in python. 我想在python中存储很多数据实例。 Each record has the following fields: username, address, salary etc... 每条记录都有以下字段:用户名,地址,薪水等。
The username should be unique. 用户名应该是唯一的。 I do a lot of searches. 我做了很多搜索。
Currently I am using a list of dictionaries, but when I insert a new item I iterate the list and check the username of each dictionary, which is O(n). 当前,我正在使用词典列表,但是当我插入一个新项时,我会对该列表进行迭代并检查每个词典的用户名,即O(n)。 Searching is O(n) too. 搜索也是O(n)。 How could I achieve that there is an index on usernames, and make the search time O(logn)? 我如何才能实现在用户名上建立索引,并使搜索时间为O(logn)?
Why not use a dictionary of dicts? 为什么不使用字典词典?
The top-level dictionary could be addressed by the username and the other dicts can be the same you have now. 顶级词典可以用用户名寻址,其他字典可以与您现在使用的相同。
A dictionary can also be scanned threw (d.values()) -- the only disadvantage is, that you can't depend on the ordering. 还可以对字典进行扫描(d.values())-唯一的缺点是,您不能依赖于排序。
Of course that is not a DB behavior, but in most cases good enough and very fast -- the access via dict is O(1). 当然,这不是数据库行为,但是在大多数情况下,它足够好且非常快-通过dict进行的访问为O(1)。
Of course you could use sqlite -- but when you just want to access via username and scan the entries, you are much faster (both in development- and runtime speed). 当然,您可以使用sqlite-但是,当您只想通过用户名访问并扫描条目时,您的速度会更快(在开发速度和运行速度上)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.