如何检索SQLAlchemy结果集的python列表?

Gav*_*ulz 24 python sqlalchemy

我有以下查询来检索单列数据:

routes_query = select(
    [schema.stop_times.c.route_number],
    schema.stop_times.c.stop_id == stop_id
).distinct(schema.stop_times.c.route_number)
result = conn.execute(routes_query)

return [r['route_number'] for r in result]
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一种更清晰的方法来检索返回的数据行的本机列表.

zzz*_*eek 47

将列表中的1元素元组列入列表的最简洁方法是:

result = [r[0] for r in result]
Run Code Online (Sandbox Code Playgroud)

要么:

result = [r for r, in result]
Run Code Online (Sandbox Code Playgroud)

  • 这几乎是我所拥有的,我想这已经足够了。谢谢你的回答。 (2认同)