我正在将一些代码从 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 中都很好用。
