From 9220f55d7859c032ffa70e6202427f0917e280e7 Mon Sep 17 00:00:00 2001 From: victorgau Date: Mon, 27 Mar 2017 18:10:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AA=AA=E6=98=8E=E6=AA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\231\250\345\236\213\345\210\245.ipynb" | 553 ++++++++++++++++-- 1 file changed, 498 insertions(+), 55 deletions(-) diff --git "a/01_basics/05. Python \347\232\204\345\256\271\345\231\250\345\236\213\345\210\245.ipynb" "b/01_basics/05. Python \347\232\204\345\256\271\345\231\250\345\236\213\345\210\245.ipynb" index 5172d1a..556bd56 100644 --- "a/01_basics/05. Python \347\232\204\345\256\271\345\231\250\345\236\213\345\210\245.ipynb" +++ "b/01_basics/05. Python \347\232\204\345\256\271\345\231\250\345\236\213\345\210\245.ipynb" @@ -47,6 +47,17 @@ "* [集合 set](http://www.iplaypython.com/jichu/set.html)" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## list -> []" + ] + }, { "cell_type": "code", "execution_count": null, @@ -62,27 +73,148 @@ "\n", "## 宣告方式\n", "list1 = []\n", - "list1 = list()\n", - "type(list1)\n", - "\n", + "type(list1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "list2 = list()\n", + "type(list2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### 索引 (indexing)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "list1 = ['B', 'A', 'C'] \n", - "list1\n", - "\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "# indexing 索引指標\n", - "list1[0] \n", - "list1[-1]\n", - "\n", + "list1[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "list1[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### 切片 (slicing)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "# slicing\n", - "list1[1:]\n", - "\n", + "list1[1:]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "# len():計算 list 長度\n", - "len(list1) \n", - "\n", + "len(list1) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "list2 = [80, 70]\n", "\n", "# list 串聯\n", - "list1 + list2\n", - "\n", + "list1 + list2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "# list 複製\n", "list1 * 3" ] @@ -91,7 +223,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "slideshow": { + "slide_type": "subslide" + } }, "outputs": [], "source": [ @@ -102,7 +237,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } }, "outputs": [], "source": [ @@ -113,7 +251,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } }, "outputs": [], "source": [ @@ -124,7 +265,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } }, "outputs": [], "source": [ @@ -135,7 +279,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } }, "outputs": [], "source": [ @@ -146,7 +293,10 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } }, "outputs": [], "source": [ @@ -166,37 +316,159 @@ "source": [ "## in 隸屬 (包含) operatior\n", "element = 'C'\n", - "element in list1\n", - "\n", + "element in list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "element = 'D'\n", - "element in list1\n", - "\n", + "element in list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "## 排序:sorted()\n", "sorted(list1)\n", - "sorted(list1, reverse=True)\n", - "\n", - "## 排序:.sort() # 注意:會蓋掉原始序列\n", + "sorted(list1, reverse=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "## 排序:.sort()\n", + "# 注意:會蓋掉原始序列\n", "list3 = list1.copy() \n", - "print(list3)\n", - "\n", + "print(list3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "list3.sort()\n", - "print(list3)\n", - "\n", + "print(list3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "list3.sort(reverse=True)\n", - "print(list3)\n", - "\n", + "print(list3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "## 顛倒 list\n", "list3.reverse()\n", - "print(list3)\n", - "\n", + "print(list3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ "## 移除特定 element\n", "list3.remove('B')\n", - "print(list3)\n", - "\n", + "print(list3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "del list3[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ "## 刪除 list\n", "del list3" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## tuple -> ()" + ] + }, { "cell_type": "code", "execution_count": null, @@ -212,21 +484,80 @@ "\n", "## tuple vs. list: \n", "# 1. tuple 生成後不可修改;list 生成後可以修改 (區別:記憶體能否動態配置)\n", - "# 2. tuple 的處理效能會優於 list,但 list 的彈性會優於 tuple\n", - "\n", + "# 2. tuple 的處理效能會優於 list,但 list 的彈性會優於 tuple" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ "## 宣告方式\n", "tup1 = ()\n", - "tup1 = tuple()\n", - "type(tup1)\n", - "\n", - "tup1 = ('physics', 'chemistry', 1997, 2000)\n", - "tup1\n", - "\n", + "type(tup1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "tup2 = tuple()\n", + "type(tup2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "tup3 = ('physics', 'chemistry', 1997, 2000)\n", + "tup3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ "# 型別轉換\n", - "tuple(list1) # list 2 tuple\n", + "tuple(list1) # list 2 tuple" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ "list(tup1) # tuple 2 list" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## dict -> {}" + ] + }, { "cell_type": "code", "execution_count": null, @@ -312,6 +643,39 @@ "dict1['Alice']" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "dict1['John']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "dict1.get(\"John\", '1234')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## set -> {}" + ] + }, { "cell_type": "code", "execution_count": null, @@ -327,20 +691,99 @@ "\n", "# 宣告 set\n", "set1 = set()\n", - "type(set1)\n", - "\n", + "type(set1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ "a = set('abc')\n", "a.add('wxyz')\n", - "a\n", - "\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ "a = set('abc')\n", "a.update('wxyz')\n", - "a\n", - "\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ "a = set('abc')\n", - "b = set('cde')\n", - "a & b # 交集\n", - "a | b # 聯集\n", + "b = set('cde')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "b" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a & b # 交集" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a | b # 聯集" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ "a - b # 差集" ] }