{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# 定义一个列表,列表虽然可以存不同类型,一般我们把相同类型的值存列表里面,不同类型存字典里(key,value)\n", "infos_list=[\"C#\",\"JavaScript\"]#定一个空列表 list=[]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C#\n", "JavaScript\n" ] } ], "source": [ "# for遍历\n", "for item in infos_list:\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C#\n", "JavaScript\n" ] } ], "source": [ "# while遍历\n", "i=0\n", "while i\n", "infos_list.extend(infos_list2)\n", "print(infos_list)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function extend:\n", "\n", "extend(...) method of builtins.list instance\n", " L.extend(iterable) -> None -- extend list by appending elements from the iterable\n", "\n" ] } ], "source": [ "#可以查看etend方法描述\n", "help(infos_list.extend)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "######################## list删除系列 ##################################" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 删除\n", "# pop()删除最后一个元素,返回删掉的元素\n", "infos_list.pop()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java', '张三']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "infos_list #查看一下列表" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['test1', 'test2']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 删除\n", "# pop(index) 删除指定下标元素,返回删掉的元素\n", "infos_list.pop(0)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Python', 'C#', 'JavaScript', 'Java', '张三']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "infos_list #查看一下列表" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "pop 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[0;32m----> 1\u001b[0;31m \u001b[0minfos_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#索引不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: pop index out of range" ] } ], "source": [ "infos_list.pop(10) #索引不存在就报错" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 'C#', 'JavaScript', 'Java']\n" ] } ], "source": [ "# remove(\"\")删除指定元素\n", "infos_list.remove(\"张三\") #没有返回值\n", "print(infos_list)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "list.remove(x): x not in list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0minfos_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"dnt\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "infos_list.remove(\"dnt\") #不存在就报错" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 'JavaScript', 'Java']\n" ] } ], "source": [ "# del xxx[index] 删除指定下标元素\n", "del infos_list[1] #没有返回值\n", "print(infos_list)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list assignment 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[0;32m----> 1\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0minfos_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m#不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" ] } ], "source": [ "del infos_list[10] #不存在就报错" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "del infos_list #删除集合(集合再访问就不存在了)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'infos_list' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\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[0minfos_list\u001b[0m \u001b[0;31m#集合再访问就不存在了\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'infos_list' is not defined" ] } ], "source": [ "infos_list #集合再访问就不存在了" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "######################## list修改系列 ##################################" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['张三', 21]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 修改 xxx[index]=xx\n", "# 注意:一般不推荐在for循环里面修改\n", "infos_list2 #查看list2列表" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['张三', 'PHP']\n" ] } ], "source": [ "infos_list2[1]=\"PHP\" #只有下标修改一种方式\n", "print(infos_list2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list assignment 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[0;32m----> 1\u001b[0;31m \u001b[0minfos_list2\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\"GO\"\u001b[0m \u001b[0;31m#不存在则异常\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" ] } ], "source": [ "infos_list2[3]=\"GO\" #不存在则异常" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['GO', 'PHP']\n" ] } ], "source": [ "# 想按值修改需要先查下标再修改\n", "infos_list2.index(\"张三\")\n", "infos_list2[0]=\"GO\"\n", "print(infos_list2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "'dnt' is not in list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0minfos_list2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"dnt\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;31m#不存在则异常\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: 'dnt' is not in list" ] } ], "source": [ "infos_list2.index(\"dnt\")#不存在则异常" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 知识面拓展: https://www.zhihu.com/question/49098374\n", "# 为什么python中不建议在for循环中修改列表?\n", "# 由于在遍历的过程中,删除了其中一个元素,导致后面的元素整体前移,导致有个元素成了漏网之鱼。\n", "# 同样的,在遍历过程中,使用插入操作,也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。\n", "# 如果使用while,则可以在面对这样情况的时候灵活应对。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "######################## list查询系列 ##################################" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "# 查询 in, not in, index, count\n", "# for扩展:https://www.cnblogs.com/dotnetcrazy/p/9102030.html#forelse\n", "names_list=[\"张三\",\"李四\",\"王二麻子\"]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['李四', '王二麻子']\n" ] } ], "source": [ "#张三在列表中执行操作\n", "if \"张三\" in names_list:\n", " names_list.remove(\"张三\")\n", "print(names_list)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['李四', '王二麻子', '大舅子']\n" ] } ], "source": [ "#查看\"大舅子\"不在列表中执行操作\n", "if \"大舅子\" not in names_list:\n", " names_list.append(\"大舅子\")\n", "print(names_list)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "#查询王二麻子的索引\n", "print(names_list.index(\"王二麻子\"))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "0\n" ] } ], "source": [ "# 统计\n", "print(names_list.count(\"大舅子\")) \n", "print(names_list.count(\"逆天\")) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "######################## list排序系列 ##################################" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# 排序(sort, reverse 逆置)\n", "num_list=[1,3,5,88,7]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7, 88, 5, 3, 1]\n" ] } ], "source": [ "#倒序\n", "num_list.reverse()\n", "print(num_list)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 5, 7, 88]\n" ] } ], "source": [ "# 从小到大排序\n", "num_list.sort()\n", "print(num_list)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[88, 7, 5, 3, 1]\n" ] } ], "source": [ "# 从大到小\n", "num_list.sort(reverse=True)\n", "print(num_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "######################## list扩展系列 ##################################" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[88, 7, 5, 3, 1, [33, 44, 22]]\n" ] } ], "source": [ "# #列表嵌套(列表也是可以嵌套的)\n", "num_list2=[33,44,22]\n", "num_list.append(num_list2)\n", "print(num_list)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[33, 44, 22]\n", "44\n" ] } ], "source": [ "# 输出\n", "print(num_list[5])\n", "print(num_list[5][1]) #嵌套列表获取值的方式" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "# 引入Null==>None\n", "a=[1,2,3,4]\n", "b=[5,6]\n", "a=a.append(b)#a.append(b)没有返回值\n", "print(a)#None" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4]\n" ] } ], "source": [ "# range扩展~创建一个整数列表\n", "# range(5)生成的序列是从0开始小于5的整数~[0,5)\n", "range_list=list(range(5))\n", "print(range_list)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n" ] } ], "source": [ "# range(1,5)生成的序列是从1开始小于5的整数~[1,5)\n", "range_list=list(range(1,5))\n", "print(range_list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "###################### List切片系列 ##############################" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n" ] } ], "source": [ "# 列表的切片操作很有用,主要跟数据相关,实际应用中和dict(后面会讲)联合使用\n", "# python切片语法:[start_index:end_index:step] (end_index取不到)\n", "top100=list(range(1,101)) #[1,101) => 1~100\n", "print(top100)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 取前10个元素\n", "top100[:10] #等价于:top100[0:10]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[91, 92, 93, 94, 95, 96, 97, 98, 99, 100]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 取最后10个元素\n", "top100[-10:]" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 前11~20(eg:第二页)\n", "top100[10:20]" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 取80~90(eg:倒数第二页)\n", "top100[-20:-10]" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 前20个数,每两个取一个(eg:隔行换样式)\n", "top100[:20:2]" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 所有数每10个取一个(eg:test的时候十里挑一)\n", "top100[::10]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 补充概念,str和tuple 也可以用切片操作哦~str上次说了,这次说下Tuple\n", "# 取前两个 返回元组\n", "(1,2,3,4,5)[:2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }