Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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
Python 从sql表中选择值对_Python_Postgresql - Fatal编程技术网

Python 从sql表中选择值对

Python 从sql表中选择值对,python,postgresql,Python,Postgresql,我有一个SQL表,有3列:“从点”、“到点”和点之间的“距离”。对于特定的点对,始终只有一个距离 | from | to | distance | |-----------------------| | 1 | 2 | 15 | | 1 | 3 | 20 | | 1 | 4 | 2 | | 3 | 2 | 16 | | 4 | 2 | 48 | | 5 | 2 | 12

我有一个SQL表,有3列:“从点”、“到点”和点之间的“距离”。对于特定的点对,始终只有一个距离

| from | to  | distance |
|-----------------------|
|  1   | 2   |   15     |
|  1   | 3   |   20     |
|  1   | 4   |   2      |
|  3   | 2   |   16     |
|  4   | 2   |   48     |
|  5   | 2   |   12     |
|  3   | 4   |   19     |
|  3   | 5   |   55     |
|  4   | 5   |   18     |
现在,我有了一个点列表,我希望获得点之间的距离,例如:

ids = [1, 2, 4]
我想返回这些点之间的所有距离:

1-2 -> 15
1-4 -> 2 
2-4 -> 48
我已经用python编写了一些代码,使我获得了这些对

    pairs=[]
    for a in ids:
        for b in ids:
            if a != b and sorted([a,b]) not in trasy:
                pairs.append(sorted([a,b]))
                print(sorted([a,b]))
我可以向SQL查询距离,但这样每对都会有一个新的查询,ID列表可能会更长(比如现在最多50个元素),我认为这不是一个好方法


我对SQL非常陌生,但我相信有一种更简单的方法,可以通过一个查询获得所有数据,这将是完美的

您可以在一个查询中获得数据对,如下所示:

with points(point_from, point_to, distance) as (
values
    (1, 2, 15),
    (1, 3, 20),
    (1, 4, 2),
    (3, 2, 16),
    (4, 2, 48),
    (5, 2, 12),
    (3, 4, 19),
    (3, 5, 55),
    (4, 1, 68),
    (4, 5, 18)
)

select least(point_from, point_to) as "from", greatest(point_from, point_to) as "to", distance
from points
where point_from in (1, 2, 4) and point_to in (1, 2, 4)

 from | to | distance 
------+----+----------
    1 |  2 |       15
    1 |  4 |        2
    2 |  4 |       48
    1 |  4 |       68
(4 rows)    
对于特定的点对,始终只有一个距离

| from | to  | distance |
|-----------------------|
|  1   | 2   |   15     |
|  1   | 3   |   20     |
|  1   | 4   |   2      |
|  3   | 2   |   16     |
|  4   | 2   |   48     |
|  5   | 2   |   12     |
|  3   | 4   |   19     |
|  3   | 5   |   55     |
|  4   | 5   |   18     |

嗯,我不这么认为。

您可以在一个查询中获得这些对,如下所示:

with points(point_from, point_to, distance) as (
values
    (1, 2, 15),
    (1, 3, 20),
    (1, 4, 2),
    (3, 2, 16),
    (4, 2, 48),
    (5, 2, 12),
    (3, 4, 19),
    (3, 5, 55),
    (4, 1, 68),
    (4, 5, 18)
)

select least(point_from, point_to) as "from", greatest(point_from, point_to) as "to", distance
from points
where point_from in (1, 2, 4) and point_to in (1, 2, 4)

 from | to | distance 
------+----+----------
    1 |  2 |       15
    1 |  4 |        2
    2 |  4 |       48
    1 |  4 |       68
(4 rows)    
对于特定的点对,始终只有一个距离

| from | to  | distance |
|-----------------------|
|  1   | 2   |   15     |
|  1   | 3   |   20     |
|  1   | 4   |   2      |
|  3   | 2   |   16     |
|  4   | 2   |   48     |
|  5   | 2   |   12     |
|  3   | 4   |   19     |
|  3   | 5   |   55     |
|  4   | 5   |   18     |

嗯,我不这么认为。

分数的顺序重要吗?您给出的SQL表示例表明它是这样的(1->4与4->1的距离不同),但您从“ids”构造的对表明它不是这样的。顺序不重要,只是在表中,它并不总是以较小的数字“from”排序,以较大的数字“to”排序。每一对总是有一个距离。我确实犯了一个错误,我写了两次4->1-纠正了它-分数的顺序重要吗?您给出的SQL表示例表明它是这样的(1->4与4->1的距离不同),但您从“ids”构造的对表明它不是这样的。顺序不重要,只是在表中,它并不总是以较小的数字“from”排序,以较大的数字“to”排序。每一对总是有一个距离。我确实犯了一个错误,我写了两次4->1-纠正了它,它已经完全符合我的要求,没有必要对“从”和“到”进行排序,我在帖子中犯了一个错误,我写了1->4和4->1-每对都有一个距离。无论如何,谢谢你的回答:)完全符合我的要求,没有必要对“from”和“to”进行排序,我在帖子中犯了一个错误,我写了1->4和4->1-每对都有一个距离。无论如何,谢谢你的回答:)