python - 使用 Psycopg 2 和 Python3 解析元组的结果列表
问题描述
如何使用Psycopg 2
in从结果的每个索引中删除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)]
先感谢您!
解决方案
列表推导结合解包元组是这里的方法:首先将结果分配给一个变量(在这种情况下我将它分配给 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]
推荐阅读
- python - 将 statsmodels logit 返回的参数约束为正值
- javascript - 在没有任何插件的情况下在 javascript 或 jquery 中延迟加载视频
- express - 猫鼬通过引用保存多个文档
- java - 从计算引擎中的 docker 容器连接到 Cloud SQL
- python - 使用像素统计遮罩
- java - 在java中访问JSONObject键的有效方法
- node.js - 如何将多个数据存储到postgres sql中?
- angular - 如何为方法编写单元测试用例 [Angular]
- corda - 无法从同一资产的进一步交易中删除参与者
- c++ - 为什么 c++ foreach 比简单的单线程循环慢?