{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "aList = [2 , 3, 1, 1, 2, 3, 'a string']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 1, 1, 2, 3, 'a string']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aList" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 1, 1, 2, 3, 'a string']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aList = list([2 , 3, 1, 1, 2, 3, 'a string'])\n", "aList" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'list' object is not callable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlist\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'a string'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: 'list' object is not callable" ] } ], "source": [ "list = list([2 , 3, 1, 1, 2, 3, 'a string'])\n", "list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表生成式" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x for x in \"abcdefg\"]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aList = []\n", "for x in 'abcdefg':\n", " aList.append(x)\n", " \n", "aList" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 8]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x*2 for x in range(1,5)]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 20, 30]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x*2 for x in range(1,20) if x%5==0]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 20, 30, 40, 50]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10, 20 ,30]\n", "b = [40, 50]\n", "a + b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [10, 20 ,30]\n", "b = [40, 50]\n", "a.extend(b)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 20, 30, 40, 50]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [10,20,30,40,50,20,30,20,30]\n", "a.index(20)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.index(20, 3)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 5000, 30, 40, 50, 20, 30, 20, 30]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1] = 5000\n", "a" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "del a[1]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 30, 40, 50, 20, 30, 20, 30]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "del a" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'a' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" ] } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a=[1,2,3,4,5,6]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.pop()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.pop(2)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [10,20,30,40,50,20,30,20,30]\n", "a.remove(20)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 30, 40, 50, 20, 30, 20, 30]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'##########'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'#' * 10" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 60, 10)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=[10,20,30,40,50,60]\n", "len(a), max(a), min(a)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([10, 20, 30], [10, 5000, 30])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aa=[10,20,30]\n", "bb=aa.copy()\n", "bb[1] = 5000\n", "aa, bb" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([10, 5000, 30], [10, 5000, 30])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aa = [10, 20, 30]\n", "bb = aa\n", "bb[1] = 5000\n", "aa, bb" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=[10,20,40,30,10,20,50,10]\n", "a.count(10)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[36, 34, 20, 17, 13, 11, 5]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=[11,20,13,34,5,36,17]\n", "a.sort(reverse=True)\n", "a" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([5, 11, 13, 17, 20, 34, 36], [36, 34, 20, 17, 13, 11, 5])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(a), a" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[36, 34, 20, 17, 13, 11, 5]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(a, key=lambda x: -x)" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }