# 创建有序字典-OrderDict

代码：

```python
from collections import OrderedDict

'''
先添加的 key-value 对排在前面，后添加的 key-value 对排在后面。这么个顺序。而不是按内部key自动排序
维护双向链表，内存是普通字典2倍
'''

odic=OrderedDict()

odic['k1']='a'
odic['k2']='dab'
odic['k3']='c'
print('odic:',odic)

# 按照第2位（value值）进行排序：lambda oi: oi[1]
sort_od = OrderedDict(sorted(odic.items(), key=lambda oi: oi[1]))
print('sort_od:',sort_od)
```

结果：

```python
odic: OrderedDict([('k1', 'a'), ('k2', 'dab'), ('k3', 'c')])
sort_od: OrderedDict([('k1', 'a'), ('k3', 'c'), ('k2', 'dab')])
```

视频：

```
wait
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/2.3.4-shi-yong-orderdict-chuang-jian-you-xu-zi-dian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
