MATLAB 结构体数组的 Python 版本是什么
I am new to Python. I want to put the result of a SQL query in a sort of matrix variable like the structure array of MATLAB. Basically a matrix that holds numbers and strings.
The variable then holds the rows and columns of the query.
How can I do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想要像 MATLAB 这样的矩阵和向量,那么最好的选择是 NumPy (和 SciPy(如果您想要更多 MATLABby 功能)
另外,如果您想制作性感的情节,我建议 Matplotlib。
If you want just matrices and vectors like MATLAB, your best bet is NumPy (and SciPy if you want more MATLABby features)
Also if you want to make sexy plots, I recommend Matplotlib.
我相信您已经发现 MySQLdb 可以将数据库中的内容导入到 Python 中。默认情况下,这将返回一个可以迭代的元组数组。如果您想要一个可以通过键访问的数据结构(如 matlab 结构),那么您需要在 Python 中使用字典。
像这样的查询
将得到以下结果
如果元组列表不是您想要的,您可以使用不同类型的 MySQLdb 游标(请参阅文档)或迭代元组列表并将其转换为其他内容。
I trust you have already discovered MySQLdb for getting the stuff from the database into Python. That will by default return an array of tuples which you can iterate over. If you want a data structure that you can access by key (as the matlab struct) then you'll need to use a dictionary in Python.
A query like
Will get you the following
If a list of tuples is not what you want, you can either use a different type of MySQLdb cursor (see the docs) or iterate over the list of tuples and convert it to something else.
如果@Matti 的答案不是你想要的,也许字典列表可以做到这一点?
然后它会被转换为类似的东西:
If @Matti's answer isn't what you want, perhaps a list of dictionaries would do it?
That would then be converted to something like: