{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. 为什么用列表" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1532445696968\n", "\n", "['hello', 'world', 98]\n" ] } ], "source": [ "a = 10 # 变量存储的是一个对象的引用\n", "# 列表存储的是多个对象的引用,每个对象有自己单独的id,type,value\n", "lst = ['hello', 'world', 98]\n", "print(id(lst))\n", "print(type(lst))\n", "print(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. 列表对象创建" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['hello', 'world', 98]\n", "hello 98\n" ] } ], "source": [ "# 创建列表的第一种方式:[]\n", "lst1 = ['hello', 'world', 98]\n", "print(lst1)\n", "print(lst1[0], lst1[-1]) # -1为最后一个元素" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['hello', 'world', 98, 'hello']\n", "hello hello\n" ] } ], "source": [ "# 创建列表的第一种方式:[]\n", "lst1 = ['hello', 'world', 98, 'hello']\n", "print(lst1)\n", "print(lst1[0], lst1[-4]) # -4为重复元素" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# 创建列表的第二种方式:使用内置函数list()\n", "lst2 = list(['hello', 'world', 98])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. 列表的特点" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\n列表的特点:\\n1.列表元素按照顺序有序排序\\n2.索引映射唯一一个数据\\n3.列表可以存储重复数据\\n4.任意数据类型混存\\n5.根据需要动态分配和回收内存\\n'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''\n", "列表的特点:\n", "1.列表元素按照顺序有序排序\n", "2.索引映射唯一一个数据\n", "3.列表可以存储重复数据\n", "4.任意数据类型混存\n", "5.根据需要动态分配和回收内存\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4. 获取指定元素的索引" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "3\n", "0\n" ] } ], "source": [ "lst = ['hello', 'world', 98, 'hello']\n", "print(lst.index('hello')) # 如果列表中有相同元素,只返回列表中相同元素的第一个元素的索引\n", "# print(list.index('Python')) # 找不到\n", "# 在指定的start和stop之间进行查找\n", "# print(lst.index('hello', 1, 3)) # 找不到\n", "print(lst.index('hello', 1, 4))\n", "print(lst.index('hello', 0, 4)) # 找第一个" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 5. 获取列表中单个元素" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "98\n", "hello\n" ] } ], "source": [ "lst = ['hello', 'world', 98, 'hello', 'world', 234]\n", "# 获取索引为2的元素(正向索引从0到N-1)\n", "print(lst[2])\n", "# 获取索引为-3的元素(逆向索引从-N到-1)\n", "print(lst[-3])\n", "# 获取索引为10的元素(指定索引不存在,抛出异常)\n", "# print(lst[10]) # IndexError: list index out of range" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 6. 获取列表中多个元素" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "原列表 1532445706440\n", "切的片段: 1532445697544\n", "[20, 30, 40, 50, 60]\n", "[20, 30, 40, 50, 60]\n", "[20, 40, 60]\n", "[10, 30, 50]\n", "[20, 40, 60, 80]\n", "[80, 70, 60, 50, 40, 30, 20, 10]\n", "[80, 70, 60, 50, 40, 30, 20, 10]\n", "[70, 50, 30]\n" ] } ], "source": [ "# 切片操作\n", "lst = [10, 20, 30, 40, 50, 60, 70, 80]\n", "# start=1,stop=6,step=1\n", "# print(lst[1:6:1])\n", "print('原列表', id(lst))\n", "lst2 = lst[1:6:1]\n", "print('切的片段:', id(lst2))\n", "print(lst[1:6]) # 默认步长为1\n", "print(lst[1:6:]) # 默认步长为1\n", "# start=1,stop=6,step=2\n", "print(lst[1:6:2])\n", "# stop=6,step=2,start采用默认,0\n", "print(lst[:6:2])\n", "# start=1,step=2,stop采用默认,最后一个元素N-1\n", "print(lst[1::2])\n", "\n", "# step为负数\n", "print(lst[::-1])\n", "# start=7,stop省略,step=-1\n", "print(lst[7::-1])\n", "# start=6,stop=0,step=-2\n", "print(lst[6:0:-2])\n", "# stop,start不能是负数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 7. 列表元素判断及遍历" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n", "False\n", "False\n", "True\n", "10\n", "20\n", "python\n", "hello\n" ] } ], "source": [ "print('p' in 'python')\n", "print('k' not in 'python')\n", "\n", "lst = [10, 20, 'python', 'hello']\n", "# 判断\n", "print(10 in lst)\n", "print(100 in lst)\n", "print(10 not in lst)\n", "print(100 not in lst)\n", "# 遍历\n", "for item in lst:\n", " print(item)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 8. 列表元素添加" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 20, 30, 100]\n", "[10, 20, 30, 100, 'hello', 'world']\n", "[10, 90, 20, 30, 100, 'hello', 'world']\n", "[10, True, False, 'hello']\n" ] } ], "source": [ "# 向列表末尾添加一个元素,标识相同,同一个列表对象\n", "lst = [10, 20, 30]\n", "lst.append(100)\n", "print(lst)\n", "\n", "# 列表末尾至少添加一个元素\n", "lst2 = ['hello', 'world']\n", "# lst.append(lst2) # lst2作为一个元素放入lst末尾\n", "lst.extend(lst2) # 添加lst2每个元素分别添加到lst末尾\n", "print(lst)\n", "\n", "# 列表的任意位置添加一个元素\n", "lst.insert(1, 90)\n", "print(lst)\n", "\n", "# 列表的任意位置添加至少一个元素(切片,切掉的位置用新的list代替)\n", "lst3 = [True, False, 'hello']\n", "lst[1:] = lst3\n", "print(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 9. 列表元素删除" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 20, 40, 50, 60, 30]\n", "[10, 40, 50, 60, 30]\n", "[10, 40, 50, 60]\n", "原列表 [10, 40, 50, 60]\n", "切片后的列表 [40, 50]\n", "[10, 60]\n", "[]\n" ] } ], "source": [ "lst = [10, 20, 30, 40, 50, 60, 30]\n", "lst.remove(30) # 从列表中溢出一个元素,如果有重复元素只移除第一个元素\n", "print(lst)\n", "# lst.remove(100) # ValueError: list.remove(x): x not in list\n", "\n", "# pop()根据索引移除元素\n", "lst.pop(1)\n", "print(lst)\n", "# lst.pop(5) # IndexError: pop index out of range 指定的索引位置不存在,抛出异常\n", "lst.pop() # 不写参数,默认删除最后一个元素\n", "print(lst)\n", "\n", "# 切片操作,删除至少一个元素,将产生一个新的列表对象\n", "new_lst = lst[1:3]\n", "print('原列表', lst)\n", "print('切片后的列表', new_lst)\n", "\n", "# 不产生新的列表对象,而是删除原列表中的内容(用空列表进行替代)\n", "lst[1:3] = []\n", "print(lst)\n", "\n", "# clear()清空列表 元素\n", "lst.clear()\n", "print(lst) # []\n", "\n", "# del删除列表 对象\n", "del lst\n", "# print(lst) # NameError: name 'lst' is not defined" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 10. 列表元素修改" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 20, 100, 40]\n", "[10, 300, 400, 500, 600, 40]\n" ] } ], "source": [ "lst = [10, 20, 30, 40]\n", "# 一次修改一个值\n", "lst[2] = 100\n", "print(lst)\n", "\n", "# 切片操作\n", "lst[1:3] = [300, 400, 500, 600]\n", "print(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 11. 列表元素排序" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "排序前的列表 [20, 40, 10, 98, 54] 1532445713608\n", "排序后的列表 [10, 20, 40, 54, 98] 1532445713608\n", "[98, 54, 40, 20, 10]\n", "[10, 20, 40, 54, 98]\n", "排序前的列表 [20, 40, 10, 98, 54]\n", "[10, 20, 40, 54, 98]\n", "[98, 54, 40, 20, 10]\n" ] } ], "source": [ "lst = [20, 40, 10, 98, 54]\n", "print('排序前的列表', lst, id(lst))\n", "# 开始排序,调用列表对象的sort方法,升序排序\n", "lst.sort()\n", "print('排序后的列表', lst, id(lst)) # 标识未更改,在原列表上进行的排序\n", "\n", "# 通过指定关键字参数,将列表中的元素进行降序排序\n", "# reverse=True 表示降序排序\n", "lst.sort(reverse=True)\n", "print(lst)\n", "# reverse=False 表示降序排序\n", "lst.sort(reverse=False)\n", "print(lst)\n", "\n", "# 使用内置函数sorted()对列表进行排序,将产生一个新的列表对象,原列表不发生改变\n", "lst = [20, 40, 10, 98, 54]\n", "print('排序前的列表', lst)\n", "# 开始排序\n", "new_lst = sorted(lst) # 产生新的列表对象\n", "print(new_lst)\n", "# 指定关键字参数,实现列表元素降序排序\n", "desc_lst = sorted(lst, reverse=True)\n", "print(desc_lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 12. 列表生成式" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[1, 4, 9, 16, 25, 36, 49, 64, 81]\n", "[2, 4, 6, 8, 10]\n" ] } ], "source": [ "# 使用列表生成式要求列表中的元素都有一定的规则\n", "lst = [i for i in range(1, 10)]\n", "print(lst)\n", "lst1 = [i*i for i in range(1, 10)]\n", "print(lst1)\n", "\n", "# 要求:列表中的元素的值为2,4,6,8,10\n", "lst2 = [i*2 for i in range(1, 6)]\n", "print(lst2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.6.3", "language": "python", "name": "python3.6.3" }, "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" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "480px" }, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 4 }