text ch139一览解构-AKA包装和-拆包/ ch139.ipynb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text ch139一览解构-AKA包装和-拆包/ ch139.ipynb相关的知识,希望对你有一定的参考价值。

{
  "cells": [
    {
      "metadata": {
        "collapsed": true,
        "trusted": false
      },
      "cell_type": "markdown",
      "source": "# Chapter 139: List destructuring (aka packing and unpacking)"
    },
    {
      "metadata": {
        "heading_collapsed": true
      },
      "cell_type": "markdown",
      "source": "## Section 139.1: Destructuring assignment"
    },
    {
      "metadata": {
        "hidden": true,
        "trusted": true
      },
      "cell_type": "code",
      "source": "a, b = (1, 2)\nprint(a, b)",
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": "1 2\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "hidden": true,
        "trusted": true
      },
      "cell_type": "code",
      "source": "a, b, c = [1]",
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "not enough values to unpack (expected 3, got 1)",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-3-ce8ca3f6da0c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: not enough values to unpack (expected 3, got 1)"
          ]
        }
      ]
    },
    {
      "metadata": {
        "hidden": true
      },
      "cell_type": "markdown",
      "source": "### Destructuring as a list"
    },
    {
      "metadata": {
        "hidden": true,
        "trusted": true
      },
      "cell_type": "code",
      "source": "head, *tail = [1, 2, 3, 4, 5]\nprint(head)\nprint(tail)",
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": "1\n[2, 3, 4, 5]\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "hidden": true
      },
      "cell_type": "markdown",
      "source": "等於下面這個:"
    },
    {
      "metadata": {
        "hidden": true,
        "trusted": true
      },
      "cell_type": "code",
      "source": "l = [1, 2, 3, 4, 5]\nhead = l[0]\ntail = l[1:]\nprint(head)\nprint(tail)",
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": "1\n[2, 3, 4, 5]\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "hidden": true
      },
      "cell_type": "markdown",
      "source": "想怎麼取就怎麼取"
    },
    {
      "metadata": {
        "hidden": true,
        "trusted": true
      },
      "cell_type": "code",
      "source": "a, b, *other, z = [1, 2, 3, 4, 5] \nprint(a, b, z, other)",
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": "1 2 5 [3, 4]\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "hidden": true
      },
      "cell_type": "markdown",
      "source": "`*_` 適合用在事前不知道 list 長度但又想要取得特定位置的 elements. 一行搞定"
    },
    {
      "metadata": {
        "hidden": true,
        "trusted": true
      },
      "cell_type": "code",
      "source": "a, *_, b = [1, 2, 3, 4, 5] \nprint(a, b)",
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": "1 5\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "hidden": true,
        "trusted": true
      },
      "cell_type": "code",
      "source": "l = (x for x in range(10000))\nhead, *_, tail = l\nprint(head, tail)",
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "text": "0 9999\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "## Section 139.2: Packing function arguments"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "def func1(arg1, arg2, arg3): \n    return (arg1,arg2,arg3)",
      "execution_count": 16,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func1(1, 2, 3)",
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 17,
          "data": {
            "text/plain": "(1, 2, 3)"
          },
          "metadata": {}
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func1(1, 2)",
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "func1() missing 1 required positional argument: 'arg3'",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-18-315c5e9d954e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m: func1() missing 1 required positional argument: 'arg3'"
          ]
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "### Packing a list of arguments"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "l = ['w', 't', 'f']\ns = 'wtf'\nprint(func1(*l))\nprint(func1(*s))",
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "stream",
          "text": "('w', 't', 'f')\n('w', 't', 'f')\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func1(*l[::-1])",
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 23,
          "data": {
            "text/plain": "('f', 't', 'w')"
          },
          "metadata": {}
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func1(*'wtf!')",
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "func1() takes 3 positional arguments but 4 were given",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-26-bbe37ddfb9cd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m'wtf!'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m: func1() takes 3 positional arguments but 4 were given"
          ]
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "### Packing keyword arguments"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "d = { 'arg1': 1, 'arg2': 2, 'arg3': 3}\nfun1(**d)",
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 29,
          "data": {
            "text/plain": "(1, 2, 3)"
          },
          "metadata": {}
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "def func3(arg1, arg2='b', arg3='c'):\n    return (arg1, arg2, arg3)",
      "execution_count": 33,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func3(*['arg1'], **{\"arg3\": \"YOOOO\"})",
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 34,
          "data": {
            "text/plain": "('arg1', 'b', 'YOOOO')"
          },
          "metadata": {}
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "## Section 139.3: Unpacking function arguments"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "def func(*args, **kwargs):\n    print(args, kwargs)",
      "execution_count": 35,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func()",
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "stream",
          "text": "() {}\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func(**{'kwarg1': 'YO', 'kwarg2': \"HI\"})",
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "text": "() {'kwarg1': 'YO', 'kwarg2': 'HI'}\n",
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "Unpack `dict` as keyword arguments"
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "func(*['arg1', 'arg2'], {'kwarg1': \"HI\"})\nfunc(*['arg1', 'arg2'], **{'kwarg1': 'HI'})",
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "stream",
          "text": "('arg1', 'arg2', {'kwarg1': 'HI'}) {}\n('arg1', 'arg2') {'kwarg1': 'HI'}\n",
          "name": "stdout"
        }
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.4",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "varInspector": {
      "window_display": false,
      "cols": {
        "lenName": 16,
        "lenType": 16,
        "lenVar": 40
      },
      "kernels_config": {
        "python": {
          "library": "var_list.py",
          "delete_cmd_prefix": "del ",
          "delete_cmd_postfix": "",
          "varRefreshCmd": "print(var_dic_list())"
        },
        "r": {
          "library": "var_list.r",
          "delete_cmd_prefix": "rm(",
          "delete_cmd_postfix": ") ",
          "varRefreshCmd": "cat(var_dic_list()) "
        }
      },
      "types_to_exclude": [
        "module",
        "function",
        "builtin_function_or_method",
        "instance",
        "_Feature"
      ],
      "position": {
        "left": "778px",
        "top": "120px",
        "width": "349px",
        "height": "281px",
        "right": "20px"
      }
    },
    "toc": {
      "nav_menu": {},
      "number_sections": true,
      "sideBar": false,
      "skip_h1_title": false,
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": "block",
      "toc_window_display": false
    },
    "gist": {
      "id": "a4e8176269051bf134fa57736ffe1eaf",
      "data": {
        "description": "ch139-List-destructuring-aka-packing-and-unpacking/ch139.ipynb",
        "public": true
      }
    },
    "_draft": {
      "nbviewer_url": "https://gist.github.com/a4e8176269051bf134fa57736ffe1eaf"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 1
}

以上是关于text ch139一览解构-AKA包装和-拆包/ ch139.ipynb的主要内容,如果未能解决你的问题,请参考以下文章

Verilog矢量包装/拆包宏

text 休息参数,解构数组和对象[JavaScript]

[ch03-01] 均方差损失函数

ES6新特性一览

[ch02-02] 非线性反向传播

ES6浅谈--解构,字符串扩展