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
在类似Python的多米诺骨牌中排序元组/查找顶点连通性_Python_Sorting - Fatal编程技术网

在类似Python的多米诺骨牌中排序元组/查找顶点连通性

在类似Python的多米诺骨牌中排序元组/查找顶点连通性,python,sorting,Python,Sorting,我有一个整数元组列表,如下所示: L=[(1,2),(7,6),(2,3),(8,5),(3,8),(5,7)] 每对定义了两个顶点之间的边,我想找到顶点的连通性。 没有循环,元组总是像多米诺骨牌一样唯一地链接起来,因此在这种情况下,排序列表应该如下所示: L_sorted=[(1,2),(2,3),(3,8),(8,5),(5,7),(7,6)] 或者 L_sorted=[1,2,3,8,5,7,6] 在Python中是否有一种使用预定义方法进行排序的有效方法 看看内置函数排序我认为任何

我有一个整数元组列表,如下所示:

L=[(1,2),(7,6),(2,3),(8,5),(3,8),(5,7)]
每对定义了两个顶点之间的边,我想找到顶点的连通性。 没有循环,元组总是像多米诺骨牌一样唯一地链接起来,因此在这种情况下,排序列表应该如下所示:

L_sorted=[(1,2),(2,3),(3,8),(8,5),(5,7),(7,6)]
或者

L_sorted=[1,2,3,8,5,7,6]

在Python中是否有一种使用预定义方法进行排序的有效方法

看看内置函数
排序

我认为任何内置工具都无法解决这个问题。第三方库可能会有所帮助。您可以使用纯python解决此问题:

L=[(1,2),(7,6),(2,3),(8,5),(3,8),(5,7)]
def domino(l):
    start = cursor = l[0][0]
    d = dict(l)
    while True:
        yield cursor
        try:
            cursor = d[cursor]
        except KeyError:
            # return here if your input may be non-cyclic.
            raise
        if cursor==start:
            return
x = list(domino(L))
# raises KeyError because 6 is dangling...
。。。或者使用。

“使用预定义方法”编写一些C代码,您可以查看。