我是靠谱客的博主 呆萌月光,最近开发中收集的这篇文章主要介绍python 数据库查询结果 数组_将MySQL结果集转换为NumPy数组的最有效方法是什么?...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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数组的最有效方法是什么?...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部