Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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 2d集连接到x.end和y.start之间_Python_Sorting_Set - Fatal编程技术网

将python 2d集连接到x.end和y.start之间

将python 2d集连接到x.end和y.start之间,python,sorting,set,Python,Sorting,Set,我在python中设置了2D,如下所示 {(59, 122), (107, 59), (122, 928), (172, 646), (254, 547), (265, 840), (547, 753), (646, 265), (717, 172), (753, 107), (840, 254), (928, 717)} 我想按如下方式重新排列这一组 {(59, 122), (122, 928), (928, 717), (717, 172), (172, 64

我在python中设置了2D,如下所示

{(59, 122),
 (107, 59),
 (122, 928),
 (172, 646),
 (254, 547),
 (265, 840),
 (547, 753),
 (646, 265),
 (717, 172),
 (753, 107),
 (840, 254),
 (928, 717)}
我想按如下方式重新排列这一组

{(59, 122),
 (122, 928),
 (928, 717),
 (717, 172),
 (172, 646),
 (646, 265),
 (265, 840),
 (840, 254),
 (254, 547),
 (547, 753),
 (753, 107),
 (107, 59)}
我想要的只是将集合连接到y.end==x.start(下一个x) 我想的是比较使用for循环的每个值,但这是非常直接的。
有没有更好更简单的代码?谢谢。

这是一个图形问题;你得到的是一个边列表,你试图在图中按顺序找到电路

一种解决方案是阅读任何方便的图形包,如
networkx
,并使用这些工具。将有很多设置,但执行将很简单

另一个是建立自己的。因为所有这些边都有很好的定向,所以你可以用dict来完成。首先把它转换成dict

edge_list = {x:y for x, y in 2Dset}
现在,只需从开始节点浏览列表,按顺序添加每条边:

circuit = []
x = 59
y = None

while y != 59:
    y = edge_list[x]
    circuit.append( (x, y) )
    x = y

for edge in circuit:
    print(edge)
结果:

{59: 122, 646: 265, 717: 172, 254: 547, 122: 928, 265: 840, 753: 107, 928: 717, 172: 646, 840: 254, 107: 59, 547: 753}
(59, 122)
(122, 928)
(928, 717)
(717, 172)
(172, 646)
(646, 265)
(265, 840)
(840, 254)
(254, 547)
(547, 753)
(753, 107)
(107, 59)

来自文档:。您可能想在这里使用
列表
元组
。谢谢,我想我在user1602492的答案中找到了解决方案:编辑:在我的情况下它不正确,对不起。。。谢谢,如果我们不考虑起始点的顺序,x可能是x=列表(EdgEyList.Kig())[0 ]。