小编Hei*_*ein的帖子

如何使用 Python 从 SQL 查询中提取列名

我想直接从 SQL 语句中提取结果表的列名:


query = """

select 
    sales.order_id as id, 
    p.product_name, 
    sum(p.price) as sales_volume 
from sales
right join products as p 
    on sales.product_id=p.product_id
group by id, p.product_name;

"""

column_names = parse_sql(query)
# column_names:
# ['id', 'product_name', 'sales_volume']
Run Code Online (Sandbox Code Playgroud)

知道要做什么parse_sql()吗?生成的函数应该能够识别别名并删除表别名/标识符(例如“sales.”或“p.”)。

提前致谢!

python sql parsing extract

5
推荐指数
1
解决办法
4862
查看次数

如何匹配两个numpy数组中包含的值对

我有两组坐标,想要找出该coo组的哪些坐标与该组中的任何坐标相同targets.我想知道coo集合中的索引,这意味着我想得到一个索引或bool的列表.

import numpy as np

coo = np.array([[1,2],[1,6],[5,3],[3,6]]) # coordinates
targets = np.array([[5,3],[1,6]]) # coordinates of targets

print(np.isin(coo,targets))

[[ True False]
 [ True  True]
 [ True  True]
 [ True  True]]
Run Code Online (Sandbox Code Playgroud)

期望的结果将是以下两个之一:

[False True True False] # bool list
[1,2] # list of concerning indices
Run Code Online (Sandbox Code Playgroud)

我的问题是,......

  • np.isin没有 - axis属性,所以我可以使用axis=1.
  • 甚至应用逻辑和输出的每一行将返回True最后一个元素,这是错误的.

我知道循环和条件,但我确信Python配备了更优雅的解决方案.

python numpy coordinates numpy-ndarray

4
推荐指数
1
解决办法
538
查看次数

标签 统计

python ×2

coordinates ×1

extract ×1

numpy ×1

numpy-ndarray ×1

parsing ×1

sql ×1