概述
I'm using MySQLdb and Python. I have some basic queries such as this:
c=db.cursor()
c.execute("SELECT id, rating from video")
results = c.fetchall()
I need "results" to be a NumPy array, and I'm looking to be economical with my memory consumption. It seems like copying the data row by row would be incredibly inefficient (double the memory would be required). Is there a better way to convert MySQLdb query results into the NumPy array format?
The reason I'm looking to use the NumPy array format is because I want to be able to slice and dice the data easily, and it doesn't seem like python is very friendly to multi-dimensional arrays in that regard.
e.g. b = a[a[:,2]==1]
Thanks!
解决方案
The fetchall method actually returns an iterator, and numpy has the fromiter method to initialize an array from an interator. So, depending on what data is in the table you could combine the two easily, or use an adapter generator.
最后
以上就是呆萌月光为你收集整理的python 数据库查询结果 数组_将MySQL结果集转换为NumPy数组的最有效方法是什么?...的全部内容,希望文章能够帮你解决python 数据库查询结果 数组_将MySQL结果集转换为NumPy数组的最有效方法是什么?...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复