Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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/8/python-3.x/16.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/0/backbone.js/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_Python 3.x - Fatal编程技术网

排序期间使用多个字段进行键计算的python方法是什么?

排序期间使用多个字段进行键计算的python方法是什么?,python,python-3.x,Python,Python 3.x,Python3删除了排序期间用于比较的,并将其替换为 引入的“主要用作从Python 2转换的程序的转换工具” 我不清楚如何使用key在python3中本地重新创建cmp行为。例如,如果我想按长度对字符串进行排序,使用按字典顺序排列的平分符,我会这样写: def compare(word1, word2): if len(word1) > len(word2): return 1 if len(word2) > len(word1):

Python3删除了排序期间用于比较的,并将其替换为

引入的“主要用作从Python 2转换的程序的转换工具”

我不清楚如何使用
key
在python3中本地重新创建
cmp
行为。例如,如果我想按长度对字符串进行排序,使用按字典顺序排列的平分符,我会这样写:

def compare(word1, word2):
    if len(word1) > len(word2):
        return 1
    if len(word2) > len(word1):
        return -1
    if word1 < word2:
        return 1
    if word2 < word1:
        return -1
    return 0

myStrings.sort(key=functools.cmp_to_key(compare), reverse=True)
class InterestingWord(str):

    def __lt__(self, x: str) -> bool:
        if len(self) < len(x):
            return True
        return super().__lt__(x)

    def __gt__(self, x: str) -> bool:
        if len(self) > len(x):
            return True
        return super().__gt__(x)

但是,当您添加更多的变量进行比较时,这似乎不能很好地扩展。在python3中排序期间使用多个字段进行键计算的Pythonc方法是什么?

对于特定的比较,可以使用元组作为键:

myStings.sort(key=lambda s:(-len(s),s),reverse=True)
对于对象类,您可能需要实现一个比较运算符(例如,
def\uu lt\uuu(self,other):
method),让排序知道如何在本地比较它们

如果无法修改对象类或比较非常复杂,则可以创建一个包装器对象,该对象使用提供的lambda实现
\uu lt\uu()
方法:

class ObjectComp:
    def __init__(self,instance,cmp):
        self.instance = instance
        self.cmp      = cmp
        
    def __lt__(self,other):
        return self.cmp(self.instance,other.instance)
        
def cmpsort(objects,cmp):
    return [obj.instance for obj in sorted(ObjectComp(o,cmp) for o in objects)]


s = cmpsort([1,11,21,12,13],cmp=lambda a,b:a%10>b%10)
print(s)
[13, 12, 1, 11, 21]

如果您有一个复杂的比较,那么您正在对字符串应用一个独特的含义,用pythonic术语来说,这可能意味着它将受益于它自己的类型。Python很乐意允许您对泛型类型进行子类化,因此在您的情况下,您可能需要这样的内容:

def compare(word1, word2):
    if len(word1) > len(word2):
        return 1
    if len(word2) > len(word1):
        return -1
    if word1 < word2:
        return 1
    if word2 < word1:
        return -1
    return 0

myStrings.sort(key=functools.cmp_to_key(compare), reverse=True)
class InterestingWord(str):

    def __lt__(self, x: str) -> bool:
        if len(self) < len(x):
            return True
        return super().__lt__(x)

    def __gt__(self, x: str) -> bool:
        if len(self) > len(x):
            return True
        return super().__gt__(x)

1.啊,那么解决方案是一个元组,其中包含打破僵局所需的尽可能多的字段?2.对于对象,您是否还需要实现
gt
eq
,等等才能使其工作?@fvrghl 1:是的。2:不需要,因为
可调用(此答案中的lambda)包含决定顺序的逻辑。它将接受该元素,比如说
e
,为每个元素创建一个元组
(-len(e),e)
,然后进行相应的排序。其中包含排序所需的
gt
eq
lt
逻辑。我认为您仍然需要实现那些用于有序集合的
gt
eq
lt
。简而言之,“pythonic”方法是让键函数为每个被比较的值返回一个值的元组,让Python的元组比较逻辑来处理这些事情。