Skip to main content
 首页 » 编程设计

Python 3 中的 Python 2 dict_items.sort()

2025年05月04日11落叶

我正在将一些代码从 Python 2 移植到 3。这是 Python 2 语法中的有效代码:

def print_sorted_dictionary(dictionary):   
    items=dictionary.items()   
    items.sort() 

在 Python 3 中,dict_items 没有方法“排序”——我如何在 Python 3 中解决这个问题?

请您参考如下方法:

使用 items = sorted(dictionary.items()),它在 Python 2 和 Python 3 中都很好用。