Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.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
在Python3中将double for循环转换为double list?_Python_Sql_Python 3.x_List Comprehension_Psycopg2 - Fatal编程技术网

在Python3中将double for循环转换为double list?

在Python3中将double for循环转换为double list?,python,sql,python-3.x,list-comprehension,psycopg2,Python,Sql,Python 3.x,List Comprehension,Psycopg2,鉴于以下结构/逻辑: for i in range(len(drug_list["drug_list_ids"])): for j in range(i + 1, len(drug_list_ids["drug_list_ids"])): with pg_get_cursor(pool) as cursor: q = """ SELECT d1.name as drug_1, d2.name as drug_2,

鉴于以下结构/逻辑:

for i in range(len(drug_list["drug_list_ids"])):
      for j in range(i + 1, len(drug_list_ids["drug_list_ids"])):
               with pg_get_cursor(pool) as cursor:
                     q = """ SELECT d1.name as drug_1, d2.name as drug_2, description
                             FROM interactions i, drugs d1, drugs d2
                             WHERE d1.id = %s
                             AND d2.id = %s
                             AND i.id1 = d1.id
                             AND i.id2 = d2.id; """
                     cursor.execute(q, (drug_list["drug_list_ids"][i], drug_list["drug_list_ids"][j]))
                     res = cursor.fetchall()

                     if res:
                         for d in res:
                             ddi_list.append(d)
我想将其转换为双列表理解,并将其传递给我的

cursor.execute(q, (drug_list["drug_list_ids"][i], drug_list["drug_list_ids"][j])) 
继续逻辑。 请告诉我怎么做

我已成功创建了第一步:

[(i,j) for i in range(len(drug_list["drug_list_ids"])) for j in range(i + 1, len(drug_list["drug_list_ids"]))]
我的字典:
{'drug_list':['dabigatran etexilate','dasatinib','lepirudin','atosiban','glycholic acid','drug_list_id':[2,3,1548,3579,8]


明确一点——我们的目标是制作药物ID的独特元组,并展示它们之间的相互作用。你在这里把事情复杂化了。您不需要在一个循环中运行多个查询,只需使用一个查询:

选择d1.name作为药物1,d2.name作为药物2,description
从互动i
i.id1上的内部连接d1=d1.id
i.id2上的内部连接d2=d2.id
哪里
d1.id在(…id列表,见下文…)
和d2.id=(…相同的id列表,见下文…)
d1.id
我在这里使用了
internaljoin
语法,而不是
FROM
子句中的多个表来将连接条件分组到一个专用位置,因此
WHERE
条件更容易推理

上面将所有
drug\u list[“drug\u list\u id”]
id传入(..)
条件中的
,然后将数据库限制为仅使用与
d1.id
子句的有效组合。这将在
d1.id
d2.id
之间生成一整套可能的(有序的)组合,就像您的for循环一样,尽管具有严格的排序顺序(使用
(81548)
(83579)
,而不是
(1548,8)
(3579,8)

实际上是Psycopg2,并将它们扩展为
的正确语法。。。在…
测试中;在这种情况下,驱动程序包括括号:

query_string = """\
    SELECT d1.name as drug_1, d2.name as drug_2, description
    FROM interactions i
    INNER JOIN drugs d1 ON i.id1 = d1.id
    INNER JOIN drugs d2 ON i.id2 = d2.id
    WHERE
        d1.id in %s
    AND d2.id in %s
    AND d1.id < d2.id
"""
with pg_get_cursor(pool) as cursor:
    cursor.execute(query_string, (
        tuple(drug_list["drug_list_ids"]),
        tuple(drug_list["drug_list_ids"])
    ))
    ddi_list = cursor.fetchall()
如果这是不可能的,那么将循环转换为列表理解有点棘手。这不是因为列表理解不能处理嵌套循环(只需按嵌套顺序从左到右列出它们),而是因为需要在循环体中使用多个语句来生成结果值。尽管如此,由于psycopg2的
cursor.execute()
,您可以使用
cursor.execute(…)或cursor
生成下一个循环的迭代器,因此您可以使用如下内容:

[v ... for ... in outer loops ... for v in (cursor.execute(...) or cursor)]
这利用了您可以直接在光标上循环以获取行的事实。无需调用cursor.fetchall()
,也无需测试特定查询是否有结果

嵌套的
for
循环可以更简洁地表示为:

但是,这一点都不高效(向数据库发送单独查询的负载),也不具有可读性。不需要,如上所示

如果您必须对id配对进行更严格的控制,则必须使用嵌套元组测试;将
d1.id
d2.id
列放入一个数组中,并使用((v1,v2)、(v3,v4),…)中的
测试(右侧)传递给
游标。execute()
作为元组的元组:

from itertools import combinations

query_string = """\
    SELECT d1.name as drug_1, d2.name as drug_2, description
    FROM interactions i
    INNER JOIN drugs d1 ON i.id1 = d1.id
    INNER JOIN drugs d2 ON i.id2 = d2.id
    WHERE
        (d1.id, d2.id) IN %s
"""

# list of [id1, id2] lists
combos = tuple(combinations(drug_list["drug_list_ids"], r=2))
with pg_get_cursor(pool) as cursor:
    cursor.execute(query_string, (combos,))
    ddi_list = cursor.fetchall()

你把事情搞得太复杂了。您不需要在一个循环中运行多个查询,只需使用一个查询:

选择d1.name作为药物1,d2.name作为药物2,description
从互动i
i.id1上的内部连接d1=d1.id
i.id2上的内部连接d2=d2.id
哪里
d1.id在(…id列表,见下文…)
和d2.id=(…相同的id列表,见下文…)
d1.id
我在这里使用了
internaljoin
语法,而不是
FROM
子句中的多个表来将连接条件分组到一个专用位置,因此
WHERE
条件更容易推理

上面将所有
drug\u list[“drug\u list\u id”]
id传入(..)
条件中的
,然后将数据库限制为仅使用与
d1.id
子句的有效组合。这将在
d1.id
d2.id
之间生成一整套可能的(有序的)组合,就像您的for循环一样,尽管具有严格的排序顺序(使用
(81548)
(83579)
,而不是
(1548,8)
(3579,8)

实际上是Psycopg2,并将它们扩展为
的正确语法。。。在…
测试中;在这种情况下,驱动程序包括括号:

query_string = """\
    SELECT d1.name as drug_1, d2.name as drug_2, description
    FROM interactions i
    INNER JOIN drugs d1 ON i.id1 = d1.id
    INNER JOIN drugs d2 ON i.id2 = d2.id
    WHERE
        d1.id in %s
    AND d2.id in %s
    AND d1.id < d2.id
"""
with pg_get_cursor(pool) as cursor:
    cursor.execute(query_string, (
        tuple(drug_list["drug_list_ids"]),
        tuple(drug_list["drug_list_ids"])
    ))
    ddi_list = cursor.fetchall()
如果这是不可能的,那么将循环转换为列表理解有点棘手。这不是因为列表理解不能处理嵌套循环(只需按嵌套顺序从左到右列出它们),而是因为需要在循环体中使用多个语句来生成结果值。尽管如此,由于psycopg2的
cursor.execute()
,您可以使用
cursor.execute(…)或cursor
生成下一个循环的迭代器,因此您可以使用如下内容:

[v ... for ... in outer loops ... for v in (cursor.execute(...) or cursor)]
这利用了您可以直接在光标上循环以获取行的事实。无需调用cursor.fetchall()
,也无需测试特定查询是否有结果

嵌套的
for
循环可以更简洁地表示为:

但是,这一点都不高效(向数据库发送单独查询的负载),也不具有可读性。不需要,如上所示

如果您必须对id配对进行更严格的控制,则必须使用嵌套元组测试;将
d1.id
d2.id
列放入一个数组中,并使用((v1,v2)、(v3,v4),…)中的
测试(右侧)传递给
游标。execute()
作为元组的元组:

from itertools import combinations

query_string = """\
    SELECT d1.name as drug_1, d2.name as drug_2, description
    FROM interactions i
    INNER JOIN drugs d1 ON i.id1 = d1.id
    INNER JOIN drugs d2 ON i.id2 = d2.id
    WHERE
        (d1.id, d2.id) IN %s
"""

# list of [id1, id2] lists
combos = tuple(combinations(drug_list["drug_list_ids"], r=2))
with pg_get_cursor(pool) as cursor:
    cursor.execute(query_string, (combos,))
    ddi_list = cursor.fetchall()