Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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/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/8/design-patterns/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_Dictionary_Comparator - Fatal编程技术网

Python自定义比较器对特定列表进行排序

Python自定义比较器对特定列表进行排序,python,sorting,dictionary,comparator,Python,Sorting,Dictionary,Comparator,我有一个类似[1,2,2,1,6]的输入列表,手头的任务是按频率排序。我已经解决了这个问题,得到的结果是[1,2,6]。 但需要注意的是,如果其中两个数字具有相同的计数,比如计数(1)=计数(2)。所以期望的输出是[2,1,6] 然后在输出数组中,2必须在1之前,因为2>1 所以对于输入[1,1,2,2,3,3],输出应该是[3,2,1]。计数是相同的,因此它们按实际值排序 这就是我所做的 输入格式: 测试用例数 列表输入 我想这对你有帮助。我添加了reverse参数,该参数在默认情况下设置为T

我有一个类似[1,2,2,1,6]的输入列表,手头的任务是按频率排序。我已经解决了这个问题,得到的结果是[1,2,6]。 但需要注意的是,如果其中两个数字具有相同的计数,比如计数(1)=计数(2)。所以期望的输出是[2,1,6] 然后在输出数组中,2必须在1之前,因为2>1

所以对于输入[1,1,2,2,3,3],输出应该是[3,2,1]。计数是相同的,因此它们按实际值排序

这就是我所做的

输入格式:

  • 测试用例数
  • 列表输入

  • 我想这对你有帮助。我添加了
    reverse
    参数,该参数在默认情况下设置为
    True
    ,因为这提供了解决方案,但我在代码中编写了代码,您可以在其中进行编辑

    代码如下:

    from collections import defaultdict # To use a dictionary, but initialized with a default value
    
    def fun(l, reverse = True):
        d = defaultdict(int)    
        # Add count
        for i in l:
            d[i] += 1 
    
        # Create a dictionary where keys are values
        new_d = defaultdict(list)
        for key,value in d.items(): 
            new_d[value].append(key)
    
        # Get frequencies
        list_freq = list(new_d.keys())
        list_freq.sort(reverse = reverse) #YOU CAN CHANGE THIS
        list_freq
    
        # Add numbers in decreasing order by frequency
        # If two integers have the same frequency, the greater number goes first
        ordered_list = []
        for number in list_freq:
            values_number = new_d[number]
            values_number.sort(reverse = reverse) # YOU CAN CHANGE THIS
            ordered_list.extend(values_number)
    
        return ordered_list
    
    示例:

    l = [1,2,2,1,6]
    fun(l)
    #Output [2,1,6]
    

    我希望这能帮助你

    我想这对你有帮助。我添加了
    reverse
    参数,该参数在默认情况下设置为
    True
    ,因为这提供了解决方案,但我在代码中编写了代码,您可以在其中进行编辑

    代码如下:

    from collections import defaultdict # To use a dictionary, but initialized with a default value
    
    def fun(l, reverse = True):
        d = defaultdict(int)    
        # Add count
        for i in l:
            d[i] += 1 
    
        # Create a dictionary where keys are values
        new_d = defaultdict(list)
        for key,value in d.items(): 
            new_d[value].append(key)
    
        # Get frequencies
        list_freq = list(new_d.keys())
        list_freq.sort(reverse = reverse) #YOU CAN CHANGE THIS
        list_freq
    
        # Add numbers in decreasing order by frequency
        # If two integers have the same frequency, the greater number goes first
        ordered_list = []
        for number in list_freq:
            values_number = new_d[number]
            values_number.sort(reverse = reverse) # YOU CAN CHANGE THIS
            ordered_list.extend(values_number)
    
        return ordered_list
    
    示例:

    l = [1,2,2,1,6]
    fun(l)
    #Output [2,1,6]
    

    我希望这能帮助你

    在这种情况下,我将采取另一种方法,而不是完全使用你的函数
    fun
    你能解释一下为什么我们必须通过reverse=False吗在这种情况下,我将采取另一种方法,而不是完全使用你的函数
    fun
    你能解释一下为什么我们必须通过reverse=False吗