Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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 如何在SQL查询之后将元组列表转换为数组以简化操作_Python - Fatal编程技术网

Python 如何在SQL查询之后将元组列表转换为数组以简化操作

Python 如何在SQL查询之后将元组列表转换为数组以简化操作,python,Python,早上好,我有以下查询,我做了一个SQL查询,得到了一个元组列表作为响应,问题是我想将该元组列表转换为一个数组以便于操作,因为在我的代码之后,需要使用数组值在数据库中更新 cursor = db_equipo.cursor() sql_interface="SELECT id_interface FROM Interface WHERE id_EquipoOrigen_id=%s" cursor.execute(sql_interface,(id_equipo,)) z=cursor.fetcha

早上好,我有以下查询,我做了一个SQL查询,得到了一个元组列表作为响应,问题是我想将该元组列表转换为一个数组以便于操作,因为在我的代码之后,需要使用数组值在数据库中更新

cursor = db_equipo.cursor()
sql_interface="SELECT id_interface FROM Interface WHERE id_EquipoOrigen_id=%s"
cursor.execute(sql_interface,(id_equipo,))
z=cursor.fetchall()
print(z)

((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))


首先,考虑使用两个索引创建一个循环,这样就可以为列表创建一个索引,为元组创建另一个索引,类似于z[x][y],但它没有得到很好的优化:

z[0][0]=3027
Z[1][0]=3028
      .
      .
我想要的是:

[3027,3028,3029,3030 ...]

您可以使用列表:

[datum[0] for datum in z]
tup = ((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))

wanted_array = [entry[0] for entry in tup]
或者,如果您希望您的代码有点花哨:

next(zip(*z))

您可以使用列表:

[datum[0] for datum in z]
tup = ((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))

wanted_array = [entry[0] for entry in tup]

我非常感激