Python 中的小表?
假设我没有超过一两打具有不同属性的对象,例如:
UID、名称、值、颜色、类型、位置
我希望能够调用 Location =“Boston”的所有对象,或类型 =“主要”。经典的数据库查询类型的东西。
大多数表解决方案(pytables、*sql)对于这么小的数据集来说确实是大材小用。我是否应该简单地迭代所有对象并为每个数据列创建一个单独的字典(在添加新对象时向字典添加值)?
这将创建如下字典:
{'Boston' : [234, 654, 234], 'Chicago' : [324, 765, 342] } - 其中这 3 位数字条目代表诸如 UID 之类的内容。
正如你所看到的,查询这个会有点痛苦。
有什么替代方案吗?
Let's say I don't have more than one or two dozen objects with different properties, such as the following:
UID, Name, Value, Color, Type, Location
I want to be able to call up all objects with Location = "Boston", or Type = "Primary". Classic database query type stuff.
Most table solutions (pytables, *sql) are really overkill for such a small set of data. Should I simply iterate over all the objects and create a separate dictionary for each data column (adding values to dictionaries as I add new objects)?
This would create dicts like this:
{'Boston' : [234, 654, 234], 'Chicago' : [324, 765, 342] } - where those 3 digit entries represent things like UID's.
As you can see, querying this would be a bit of a pain.
Any thoughts of an alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于小的关系问题,我喜欢使用 Python 的内置 集合。
对于 location = 'Boston' OR type = 'Primary' 的示例,如果您有以下数据:
您可以执行
WHERE ... OR ...
查询,如下所示:或者仅使用一个表达式:
您还可以使用 itertools 中的函数来创建相当有效的数据查询。例如,如果您想要执行类似于
GROUP BY city
的操作:您还可以手动构建索引(构建将位置映射到用户 ID 的
dict
)以加快速度。如果这变得太慢或笨重,那么我可能会切换到 sqlite,它现在已包含在内在 Python (2.5) 标准库中。For small relational problems I love using Python's builtin sets.
For the example of location = 'Boston' OR type = 'Primary', if you had this data:
You can do the
WHERE ... OR ...
query like this:Or with just one expression:
You can also use the functions in itertools to create fairly efficient queries of the data. For example if you want to do something similar to a
GROUP BY city
:You could also build indexes manually (build a
dict
mapping Location to User ID) to speed things up. If this becomes too slow or unwieldy then I would probably switch to sqlite, which is now included in the Python (2.5) standard library.我不认为 sqlite 会“大材小用”——它从 2.5 开始就带有标准的 Python,所以不需要安装东西,它可以在内存或本地磁盘文件中创建和处理数据库。真是的,怎么可能更简单呢……?如果您想要内存中的所有内容(包括初始值),并且想要使用字典来表达这些初始值,例如...:
现在您的内存中微小的“db”已准备就绪。当然,在磁盘文件中创建数据库和/或从文本文件、CSV 等读取初始值并不困难。
查询特别灵活、简单和贴心,例如,您可以随意混合字符串插入和参数替换...:
发出
[u'Mr.福,你先生。 Quux']
,就像更优雅但等效的查询一样,您想要的查询只是:
等等。说真的——有什么不喜欢的呢?
I do not think sqlite would be "overkill" -- it comes with standard Python since 2.5, so no need to install stuff, and it can make and handle databases in either memory or local disk files. Really, how could it be simpler...? If you want everything in-memory including the initial values, and want to use dicts to express those initial values, for example...:
and now your in-memory tiny "db" is ready to go. It's no harder to make a DB in a disk file and/or read the initial values from a text file, a CSV, and so forth, of course.
Querying is especially flexible, easy and sweet, e.g., you can mix string insertion and parameter substitution at will...:
emits
[u'Mr. Foo', u'Mr. Quux']
, just like the more elegant but equivalentand your desired query's just:
etc. Seriously -- what's not to like?
如果数据量确实很小,我就不会费心索引,可能只是编写一个辅助函数:
这将允许像这样的漂亮查询:
If it's really a small amount of data, I'd not bother with an index and probably just write a helper function:
Which will allow nice looking queries like this: