使用Psycopg 2和Python3解析已重新组合的元组列表

问题描述 投票:0回答:2

如何使用Psycopg 2中的Python 3从结果的每个索引中删除?

以下是查询的结果 - 根据fetchall(),我的理解是一个元组列表。

import psycopg2
conn = psycopg2.connect(
    host="localhost", database="mydb_name", user="postgres", password="mydb_pwd);

# Activate connection cursor
cur = conn.cursor()
cur.execute(
    """
    SELECT
        (dmp).path[1],
        degrees(
            ST_Azimuth(
                (dmp).geom,
                ST_SetSRID(ST_MakePoint(391139.27, 5821816.69, 1.6), 25833)
            )
        ) AS azm 
    FROM 
    (SELECT ST_DumpPoints(geometry) AS dmp
     FROM surface_geometry
     Where cityobject_id=95) q
    """)
BuildingsAzimuth = cur.fetchall()

以下是结果的简短视图。

[(1, 218.030625163645),
 (1, 218.002520152173),
 (1, 218.002523848173),
 (1, 218.030628886541),
 (1, 218.030625163645),
 (1, 218.043760742269),
 (1, 218.030625163645),
 (1, 218.030628886541),
         .
         .
         .
 (1, 218.439989653911),
 (1, 218.002523848173),
 (1, 218.002520152173)]

先感谢您!

python list parsing psycopg2
2个回答
1
投票

结合解包元组的列表推导是这里的方法:首先将结果分配给变量(在这种情况下我将其分配给result_list),然后使用迭代两元素的列表推导(索引和值)列表中的元组,只返回结果的值,而不是索引。

cleaned_result = [value for index, value in results_list] #Creates a new list with values you are interested in, without the indices.  
print(cleaned_result[0:3])

结果是:

[218.030625163645, 218.002520152173, 218.002523848173]
© www.soinside.com 2019 - 2024. All rights reserved.