{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 字典排序输出"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python 3.7以前的版本中,字典本身是无序的,需要排序时,可以将字典的元素、键或值转为列表再排序输出。 \n",
"3.7以后的版本字典顺序确保为插入顺序。 \n",
"字典视图dict.keys()、dict.values()和dict.items()都是可逆的,同时可以作为排序函数sorted()的参数,排序返回结果为列表。 \n",
"语法如下:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"sorted(iterable, *, key=None, reverse=False)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 字典名或dict.keys()为参数可返回键的排序列表"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Ana', 'Bob', 'Jack', 'Tom']\n",
"['Ana', 'Bob', 'Jack', 'Tom']\n"
]
}
],
"source": [
"info = {'Tom': 21, 'Bob': 18, 'Jack': 23, 'Ana': 20}\n",
"sort_info = sorted(info) # 直接对字典排序时,等价于对info.keys()排序 \n",
"sort_key = sorted(info.keys()) # 对info.keys()排序 \n",
"print(sort_key) # ['Ana', 'Bob', 'Jack', 'Tom']\n",
"print(sort_info) # ['Ana', 'Bob', 'Jack', 'Tom']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/29.png\" style=\"zoom:80%;\">\n",
"\n",
"### dict.values()为参数可返回值的排序列表"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[18, 20, 21, 23]\n"
]
}
],
"source": [
"info = {'Tom': 21, 'Bob': 18, 'Jack': 23, 'Ana': 20}\n",
"sort_value = sorted(info.values()) # 对info.values()排序 \n",
"print(sort_value) # [18, 20, 21, 23]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/30.png\" style=\"zoom:80%;\">\n",
"\n",
"### dict.items()为参数可对列表元素进行排序,默认根据键值排序"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Ana', 20), ('Bob', 18), ('Jack', 23), ('Tom', 21)]\n",
"<class 'list'>\n"
]
}
],
"source": [
"info = {'Tom': 21, 'Bob': 18, 'Jack': 23, 'Ana': 20}\n",
"sort_item = sorted(info.items()) # 对info.items()排序,默认根据键值排序 \n",
"print(sort_item) \n",
"print(type(sort_item))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/31.png\" style=\"zoom:50%;\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"参数key是一个函数,用来选取参与比较的元素,可结合lambda表达式; \n",
"参数reverse则是用来指定排序是升序还是降序,reverse = True是降序,reverse = False是升序。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"info = {'Tom': 21, 'Bob': 18, 'Jack': 23, 'Ana': 20}\n",
"sort_age = sorted(info.items(), key=lambda item: item[1], reverse = True)\n",
"print(sort_age)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"images/ch7/32.png\" style=\"zoom:80%;\">"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}