master
/ 7.2.4内置函数与方法.ipynb

7.2.4内置函数与方法.ipynb @master

d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
c7ab69a
 
 
 
 
 
 
 
 
 
 
 
d487d71
c7ab69a
d487d71
 
 
 
c7ab69a
 
 
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 内置函数与方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了前面介绍的函数和方法外,字典还包含了一系列其他的内置函数和方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典内置函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 函数           | 描述                                               |\n",
    "| :-------------- | :-------------------------------------------------- |\n",
    "| len(dict)      | 计算字典元素个数,即键的总数。                     |\n",
    "| str(dict)      | 返回字典可打印的字符串表示。                       |\n",
    "| list(dict)      | 返回字典中使用的所有键的列表。                       |\n",
    "| reversed(dict)      | 返回一个逆序获取字典键的迭代器。Python 3.8 版新增功能。                     |\n",
    "| type(variable) | 返回输入的变量类型,如果变量是字典就返回字典类型。 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "{'李明': 13988887777, '张宏': '13866668888'}\n",
      "['李明', '张宏']\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "tel_book = {'李明': 13988887777, '张宏': '13866668888'}\n",
    "length = len(tel_book)                    # 返回元素个数2\n",
    "tel_book_str = str(tel_book)              # 返回字符串'{'李明': \"13988887777', '张宏': '13866668888'}\"\n",
    "keys_ls = list(tel_book)                  # 返回键列表['李明', '张宏']\n",
    "data_type = type(tel_book)                # 返回变量类型<class 'dict'>\n",
    "print(length, tel_book_str, keys_ls, data_type, sep='\\n')\n",
    "# reversed_keys = list(reversed(tel_book))  # 返回一个逆序获取字典键的迭代器dict_reversekeyiterator,可转换为列表。\n",
    "# print(length, tel_book_str, keys_ls, data_type, reversed_keys, sep='\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"images/ch7/28.png\" style=\"zoom:50%;\">\n",
    "\n",
    "### 字典内置方法\n",
    "\n",
    "| 函数                                 | 描述                                                         |\n",
    "| :------------------------------------ | :------------------------------------------------------------ |\n",
    "| dict.clear()                         | 删除字典内所有元素                                           |\n",
    "| dict.copy()                          | 返回一个字典的浅复制                                         |\n",
    "| dict.fromkeys(iterable[,   value])   | 创建一个新字典,以序列iterable中元素做字典的键,value为字典所有键对应的初始值 |\n",
    "| dict.get(key,   default=None)        | 返回指定键的值,如果值不在字典中返回default值                |\n",
    "| dict.keys()                          | 以可迭代数据返回一个字典所有的键                             |\n",
    "| dict.values()                        | 以可迭代数据返回字典中的所有值                               |\n",
    "| dict.items()                         | 以可迭代数据返回可遍历的(键, 值) 元组                        |\n",
    "| dict.pop(key[,   default])           | 如果键key存在,返回键对应的值并移除键值对,如key不存在,返回default。 |\n",
    "| dict.popitem()                       | 按后进先出顺序,移除并返回最后一个键值对(早期的版本随机移除一个元素) |\n",
    "| dict.setdefault(key,   default=None) | 返回指定键的值,,但如果键不存在于字典中,将会添加键并将值设为default。 |\n",
    "| dict.update(dict2)                   | 把字典dict2的键/值对更新到dict里                             |"
   ]
  },
  {
   "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": 2
}