Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MysqlDb如何在列表中存储fetchone()的结果?_Python_Mysql Python - Fatal编程技术网

Python MysqlDb如何在列表中存储fetchone()的结果?

Python MysqlDb如何在列表中存储fetchone()的结果?,python,mysql-python,Python,Mysql Python,我需要将fetchone()的结果作为单独的元素存储在python的列表中。详细地说,如果fetchone()检索('1L','chuvi','21L','student'),我如何将检索到的结果存储在以下格式的列表中self.res=[1L,'chuvi',21,'student']。 我尝试了以下代码: self.res=[] self.res.append(con.fetchone()) print self.res 但我得到的结果是:[(1L,'chuvi',21L,'st

我需要将fetchone()的结果作为单独的元素存储在python的列表中。详细地说,如果fetchone()检索('1L','chuvi','21L','student'),我如何将检索到的结果存储在以下格式的列表中self.res=[1L,'chuvi',21,'student']。 我尝试了以下代码:

self.res=[]  
self.res.append(con.fetchone())  
print self.res  

但我得到的结果是:[(1L,'chuvi',21L,'student')]。如何将其作为单独的元素存储在列表中???

fetchone的结果是一个元组。如果将其附加到列表中,则会得到一个包含元组的列表。如果您想创建一个具有相同内容的列表,只需执行
self.res=list(con.fetchone())
。但在很多情况下,你不需要这样做;您可以直接使用元组。

尝试使用列表的扩展方法:

>>> a = (1,2,3)
>>> res = []
>>> res.extend(a)
>>> res
[1, 2, 3]
>>> list(a)
[1, 2, 3]

希望它能对你有所帮助。

但是布伦我还是得到了同样的结果[(1L,'chuvi',21L,'student')]。我需要将元组作为单独的内容存储在一个列表中,如[1L,'chuvi',21,'student']。我该怎么做?你是说当你做
self.res=list(con.fetchone())
时,它就不包含你想要的东西了?我觉得很难相信。当您尝试这样做时,您可以发布代码并输出吗?整个元组存储为列表的第一个元素。但我只需要将元组的元素作为单独的元素存储在列表中。我只需要将元组的内容拆分并作为单独的元素存储在列表中。这在正常情况下有效。但是当我尝试使用fetchone()时,列表包含整个元组作为第一个元素。但我需要将元组内容拆分并存储为列表中的单独元素