繁体   English   中英

在 Python 字典中排序 function

[英]Sorted function in Python dictionary

我有一本字典,如果有任何值平局,我需要打印首先按“值”排序的字典内容,然后我需要使用“键”作为平局断路器,以便打印具有相同值的键字母顺序。

final = OrderedDict(sorted(mydict.items(), key=lambda x: (x[1], x[0]), reverse=True))

for key, value in final.items():
    print(f'{key} : {value}')

但是当打印命令运行时,它将显示 output 就像没有按预期排序相同的值:

Action : 3
Romance : 2
Horror : 2
History : 2
Comedy : 2
Adventure : 1

它应该打印的方式必须是这样的:

Action : 3
Comedy : 2
History : 2
Horror : 2
Romance : 2
Adventure : 1

关于如何纠正这个问题的任何想法?

{k: v for k, v in sorted(mydict.items(), key=lambda item: (-item[1], item[0]), reverse=False)}

在根据key对序列进行排序应用reverse 您只希望x[1]上的比较被反转,而不是最终排序。 我将使用functools.cmp_to_key将比较 function 转换为密钥 function。

from functools import cmp_to_key

# Redefined from Python 2 since Python 3 dropped it
def cmp(x, y):
    return -1 if x < y else 0 if x == y else 1

def genre_sort(x, y):
    # A bigger count is a smaller genre; otherwise, sort alphabetically
    # by name
    return cmp(y[1], x[1]) or cmp(x[0], y[0])
    
final = OrderedDict(sorted(mydict.items(), key=cmp_to_key(genre_sort)))

您只想反转x[1]的排序,所以不要在这里传递reverse=True ,而是使用负数技巧:

final = OrderedDict(sorted(mydict.items(), key=lambda x: (-x[1], x[0])))

当已经有其他三个答案时,为什么要这个答案?

在我看来,其他答案都没有暴露问题中描述的问题的核心,顺便说一句,这里已经充分回答了:https://stackoverflow.com/questions/20145842/python-sorting-by-multiple-criteria 在这里 https://stackoverflow.com/questions/55866762/how-to-sort-a-list-of-strings-in-reverse-order-without-using-reverse-true-parame使其成为关闭的候选者作为副本。

Python 的特点是可以解决以不同排序顺序对列进行排序的问题,即

Python 的排序是稳定的,允许您使用连续排序,首先使用最右边的标准,然后是下一个,依此类推(Martijn Pieters)

Python 的排序算法是稳定的,这意味着相等的元素保持它们的相对顺序。 因此,您可以对第二个元素使用第一次排序(按升序排序),然后再次排序,仅对第一个元素并以相反的顺序排序。

我已将问题中列出的字典更改为仅包含字符串值,不允许在键 function 中使用带有负数值的“技巧”,以实现所需的结果来演示上述内容。

下面的代码:

mydict = { 
    'Romance'   : '2', 
    'Adventure' : '1', 
    'Action'    : '3', 
    'Horror'    : '2', 
    'History'   : '2',
    'Comedy'    : '2',
}
mylist = list(mydict.items())
print(mylist)
print()
mylist = sorted(mylist)
print(mylist)
mslist = sorted(mylist, key=lambda x: (x[1]), reverse=True)
print(mslist)
from collections import OrderedDict
final = OrderedDict(mslist)
for key, value in final.items():
    print(f'  {key:10} : {value}')

创建以下 output:

[('Romance', '2'), ('Adventure', '1'), ('Action', '3'), ('Horror', '2'), ('History', '2'), ('Comedy', '2')]

[('Action', '3'), ('Adventure', '1'), ('Comedy', '2'), ('History', '2'), ('Horror', '2'), ('Romance', '2')]
[('Action', '3'), ('Comedy', '2'), ('History', '2'), ('Horror', '2'), ('Romance', '2'), ('Adventure', '1')]
  Action     : 3
  Comedy     : 2
  History    : 2
  Horror     : 2
  Romance    : 2
  Adventure  : 1

展示了我在这里所说的内容。

从:

[('Action', '3'), ('Adventure', '1'), ('Comedy', '2'), ('History', '2'), ('Horror', '2'), ('Romance', '2')]
[('Action', '3'), ('Comedy', '2'), ('History', '2'), ('Horror', '2'), ('Romance', '2'), ('Adventure', '1')]

可以看出,第二个排序仅移动'('Adventure', '1')'保持所有其他项目的顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM