{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuple và List\n", "\n", "## Tuple" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "te = ()\n", "te" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 'one', 3)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (2, 'one', 3)\n", "t" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 'one', 3, 5, 6)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t + (5, 6)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "t[1] = 4" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('one',)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1:2]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('one', 3)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1:3]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = 2\n", "y = 5\n" ] } ], "source": [ "x = 5\n", "y = 2\n", "(x, y) = (y, x) # swap variable values\n", "print('x =', x)\n", "print('y =', y)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "quotient = 0\n", "remainder = 4\n" ] } ], "source": [ "def quotient_and_remainder(x, y):\n", " q = x // y\n", " r = x % y\n", " return (q, r)\n", "\n", "(quot, rem) = quotient_and_remainder(4, 5)\n", "print('quotient =', quot)\n", "print('remainder =', rem)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Small = 1\n", "Large = 7\n", "Unique words = 3\n" ] } ], "source": [ "def get_data(aTuple):\n", " nums = ()\n", " words = ()\n", " for t in aTuple:\n", " nums = nums + (t[0],) \n", " if t[1] not in words: \n", " words = words + (t[1],)\n", " min_nums = min(nums)\n", " max_nums = max(nums)\n", " unique_words = len(words)\n", " return (min_nums, max_nums, unique_words)\n", "\n", "\n", "(small, large, words) = get_data(((1, 'mine'), \n", " (3, 'yours'),\n", " (5, 'ours'),\n", " (7, 'mine')))\n", "\n", "print('Small =', small)\n", "print('Large =', large)\n", "print('Unique words = ', words)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_list = []\n", "a_list" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 'a', 4, True]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b_list = [2, 'a', 4, True]\n", "b_list" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n", "4\n" ] }, { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\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[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "L = [2, 1, 3]\n", "print(len(L))\n", "print(L[0])\n", "print(L[2] + 1)\n", "print(L[3])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 5, 3]\n" ] } ], "source": [ "L[1] = 5\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "total = 0\n", "for i in range(len(L)):\n", " total += L[i]\n", "print(total)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "total = 0\n", "for i in L:\n", " total += i\n", "print(total)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Một số thao tác với List\n", "\n", "### Thêm" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 5, 3]\n" ] } ], "source": [ "print(L)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 5, 3, 5]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L.append(5)\n", "L" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 1, 3, 4, 5, 6]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L1 = [2, 1, 3]\n", "L2 = [4, 5, 6]\n", "L3 = L1 + L2\n", "L3" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 1, 3]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L1" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L2" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "L1.extend([0, 6])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 1, 3, 0, 6]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Xoá" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 1, 3, 6]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del(L1[3])\n", "L1" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "a = L1.pop()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 1, 3]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L1" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 6, 3, 7, 0]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [2, 1, 3, 6, 3, 7, 0]\n", "L.remove(2)\n", "L" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 6, 3, 7, 0]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L.remove(3)\n", "L" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 7, 0]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del(L[1])\n", "L" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 7]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L.pop()\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Chuyển đổi thành chuỗi và ngược lại" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I', ' ', '<', '3', ' ', 'c', 's']" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'I <3 cs'\n", "list(s)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I ', '3 cs']" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.split('<')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I', '<3', 'cs']" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.split()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = ['a', 'b', 'c']\n", "''.join(L)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a_b_c'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'_'.join(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Một số thao tác khác" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 6, 0, 3]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [9, 6 , 0, 3]\n", "L" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 3, 6, 9]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(L)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 6, 0, 3]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 3, 6, 9]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L.sort()\n", "L" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 6, 3, 0]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L.reverse()\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sửa đổi, gán và sao chép - mutation, aliasing, cloning" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "warm = ['red', 'yellow', ' orange']\n", "hot = warm # aliasing" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red', 'yellow', ' orange']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hot" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red', 'yellow', ' orange']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "warm" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red', 'yellow', ' orange', 'pink']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hot.append('pink')\n", "hot" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['red', 'yellow', ' orange', 'pink']" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "warm" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['blue', 'green', 'grey']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cool = ['blue', 'green', 'grey']\n", "chill = ['blue', 'green', 'grey']\n", "cool" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['blue', 'green', 'grey']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chill" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['blue', 'green', 'blue']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chill[2] = 'blue' # mutating\n", "chill" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['blue', 'green', 'grey']" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cool" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['blue', 'green', 'grey']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chill = cool[:] # cloning\n", "chill" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['blue', 'green', 'grey', 'black']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chill.append('black')\n", "chill" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['orange', 'red', 'yellow']\n", "None\n" ] } ], "source": [ "warm = ['red', 'yellow', 'orange']\n", "sortedwarm = warm.sort()\n", "print(warm)\n", "print(sortedwarm)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['grey', 'green', 'blue']\n", "['blue', 'green', 'grey']\n" ] } ], "source": [ "cool = ['grey', 'green', 'blue']\n", "sortedcool = sorted(cool)\n", "print(cool)\n", "print(sortedcool)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List of list of list" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['yello', 'orange']]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "warm = ['yello', 'orange']\n", "hot = ['red']\n", "brightcolors = [warm]\n", "brightcolors" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['yello', 'orange'], ['red']]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "brightcolors.append(hot)\n", "brightcolors" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['red', 'pink']\n", "[['yello', 'orange'], ['red', 'pink']]\n" ] } ], "source": [ "hot.append('pink')\n", "print(hot)\n", "print(brightcolors)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['red', 'pink', 'yello', 'orange']\n", "['red', 'pink']\n" ] } ], "source": [ "print(hot + warm)\n", "print(hot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mutation and Iteration" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def remove_dups(L1, L2):\n", " for e in L1:\n", " if e in L2:\n", " L1.remove(e)\n", " \n", "L1 = [1, 2, 3, 4]\n", "L2 = [1, 2, 5, 6]\n", "remove_dups(L1, L2)\n", "L1" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def remove_dups_new(L1, L2):\n", " L1_copy = L1[:] # clone\n", " for e in L1_copy:\n", " if e in L2:\n", " L1.remove(e)\n", " \n", "L1 = [1, 2, 3, 4]\n", "L2 = [1, 2, 5, 6]\n", "remove_dups_new(L1, L2)\n", "L1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hàm cũng là đối tượng" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3.4]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def applyToEach(L, f):\n", " '''\n", " assumes L is a list, f a function\n", " mutates L by replacing each element, e, of L by f(e)\n", " '''\n", " for i in range(len(L)):\n", " L[i] = f(L[i])\n", " \n", "L = [1, -2, 3.4]\n", "applyToEach(L, abs)\n", "L" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "applyToEach(L, int)\n", "L" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 6]" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fact(n):\n", " if n == 1:\n", " return 1\n", " else:\n", " return n*fact(n-1)\n", " \n", "applyToEach(L, fact)\n", "L" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 13]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fib(x):\n", " if x == 0 or x == 1:\n", " return 1\n", " else:\n", " return fib(x - 1) + fib(x - 2)\n", "\n", "applyToEach(L, fib)\n", "L" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "4\n", "24\n", "5\n" ] } ], "source": [ "def applyFuns(L, x):\n", " for f in L:\n", " print(f(x))\n", " \n", "applyFuns([abs, int, fact, fib], 4)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map(abs, [1, -2, 3, -4])" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for elt in map(abs, [1, -2, 3, -4]):\n", " print(elt)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "28\n", "9\n" ] } ], "source": [ "L1 = [1, 28, 36]\n", "L2 = [2, 57, 9]\n", "for elt in map(min, L1, L2):\n", " print(elt)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = []\n", "for elt in map(abs, [1, -2, 3, -4]):\n", " a += [elt]\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - -\n", "[Trước: Hàm](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/4%20-%20H%C3%A0m.ipynb) | \n", "[Mục lục](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/index.ipynb) | \n", "[Tiếp: Dictionary]()" ] } ], "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }