---
title: "python dict根据key和value排序"
author: "heipark"
date: "2017-02-14T19:37:28Z"
tags: []
categories: ["编程语言"]
slug: "python-dict-key-value"
draft: false
lastmod: "2017-02-14T19:38:04Z"
id: 2357087
url: "https://heipark.iteye.com/blog/2357087"
---

## 根据key排序

```
a = {'a':1, 'c':2, 'b':3}
[(k,a[k]) for k in sorted(a.keys())]
#返回 [('a', 1), ('b', 3), ('c', 2)]
```

 

## 根据value排序

```
a = {'a':1, 'c':2, 'b':3}
sorted(a.items(), key=lambda d:d[1], reverse = True)

 #返回list：[('b', 3), ('c', 2), ('a', 1)]
```