发布于 1年前
Python 用键对字典进行排序
d = {'one': 1, 'four': 4, 'eight': 8}
result = {key: d[key] for key in sorted(d.keys())}
print(result)
# {'eight': 8, 'four': 4, 'one': 1}
d = {'one': 1, 'four': 4, 'eight': 8}
result = {key: d[key] for key in sorted(d.keys())}
print(result)
# {'eight': 8, 'four': 4, 'one': 1}