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
使用cmp_to_键的Python排序不会交换某些项_Python_Sorting - Fatal编程技术网

使用cmp_to_键的Python排序不会交换某些项

使用cmp_to_键的Python排序不会交换某些项,python,sorting,Python,Sorting,我的程序收到了一个列表,该列表将使用作为dict传递的“直接依赖项”进行排序。一个必须出现在它所依赖的其他项之后 我正在使用functools.cmp_to_键返回1或-1,当比较的项目相互依赖时,或当它们不依赖时返回0: 这是我的MCVE: 依赖项定义: def dependencies(): return { "B" : ["A"], "C" : ["B","A"], "D" : ["C","B"] } 比较功能: def dependency_cmp(module_left, m

我的程序收到了一个列表,该列表将使用作为
dict
传递的“直接依赖项”进行排序。一个必须出现在它所依赖的其他项之后

我正在使用
functools.cmp_to_键
返回
1
-1
,当比较的项目相互依赖时,或当它们不依赖时返回
0

这是我的MCVE:

依赖项定义:

def dependencies():
    return { "B" : ["A"], "C" : ["B","A"], "D" : ["C","B"] }
比较功能:

def dependency_cmp(module_left, module_right):
    if module_right in dependencies() and module_left in dependencies()[module_right]:
        print( module_right + " needs " + module_left )
        return -1
    elif module_left in dependencies() and module_right in dependencies()[module_left]:
        print( module_left + " needs " + module_right )
        return 1
    else:
        print( module_left + " and " + module_right + " are independent" )
        return 0
def doSort( the_list ):
    original = the_list[:]
    the_list.sort(key=cmp_to_key(lambda left, right: dependency_cmp(left,right)))
    print( "Dependencies [" + ",".join(original) + "] sorted to [" + ",".join(the_list) + "]" )
排序功能:

def dependency_cmp(module_left, module_right):
    if module_right in dependencies() and module_left in dependencies()[module_right]:
        print( module_right + " needs " + module_left )
        return -1
    elif module_left in dependencies() and module_right in dependencies()[module_left]:
        print( module_left + " needs " + module_right )
        return 1
    else:
        print( module_left + " and " + module_right + " are independent" )
        return 0
def doSort( the_list ):
    original = the_list[:]
    the_list.sort(key=cmp_to_key(lambda left, right: dependency_cmp(left,right)))
    print( "Dependencies [" + ",".join(original) + "] sorted to [" + ",".join(the_list) + "]" )
然后,当我打电话时:

doSort( ["C","D","B","A"] )
doSort( ["B","E","A"] )
它输出:

D needs C
D needs B
D needs B
C needs B
C needs A
B needs A
Dependencies [C,D,B,A] sorted to [A,B,C,D]
这是正确的,但是:

E and B are independent
A and E are independent
Dependencies [B,E,A] sorted to [B,E,A]
这是不正确的,我希望输出是
[A,B,E]
[E,A,B]
[A,E,B]
,因为
B
需要
A
E
不需要任何

但由于
B
E
E
A
顺序正确,排序功能不会对照
A
检查
B
。这是有道理的,但那绝对不是我想要的。此问题可能是由返回0引起的。熟悉C++,其中代码>排序< /COD>函数返回布尔值而不是整数…


正确排列这些列表的最佳策略是什么?

Thanx至Aran Fey注释,并使用以下位置的toposort代码:

最后我们得到了预期的输出:

Dependencies [C,D,B,A] sorted to [A,B,C,D]
Dependencies [B,E,A] sorted to [E,A,B]

这是一个很好的例子。正常的
排序
功能不适合此项工作。@Aran Fey:谢谢!这篇文章显然为我提供了一个工作功能: