{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Code:https://github.com/lotapp/BaseCode\n",
"\n",
"**多图旧版**:https://www.cnblogs.com/dunitian/p/9156097.html\n",
"\n",
"**在线预览**:http://github.lesschina.com/python/base/pop/3.list_tuple_dict_set.html\n",
"\n",
"今天说说`List、Tuple、Dict、Set`。POP部分还有一些如Func、IO(也可以放OOP部分说)然后就说说面向对象吧。\n",
"\n",
"先吐槽一下:Python面向对象真心需要规范,不然太容易走火入魔了 -_-!!! 汗,下次再说。。。\n",
"\n",
"## 1.Python列表相关\n",
"\n",
"### 1.1.列表定义、遍历\n",
"\n",
"`info_list=[]` #空列表\n",
"\n",
"`infos_list=[\"C#\",\"JavaScript\"]`\n",
"\n",
"遍历和之前一样,`for` 或者 `while` 都可以\n",
"\n",
"for扩展:https://www.cnblogs.com/dunitian/p/9103673.html#forelse"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# 定义一个列表,列表虽然可以存不同类型,一般我们把相同类型的值存列表里面\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": 8,
"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": [
"#可以查看extend方法描述\n",
"help(infos_list.extend)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3.列表删除\n",
"\n",
"`infos_list.pop()` # 删除最后一个\n",
"\n",
"`infos_list.pop(0)` # 删除指定索引,不存在就报错"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 删除\n",
"# pop()删除最后一个元素,返回删掉的元素\n",
"infos_list.pop()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java', '张三']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"infos_list #查看一下列表"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['test1', 'test2']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 删除\n",
"# pop(index) 删除指定下标元素,返回删掉的元素\n",
"infos_list.pop(0)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Python', 'C#', 'JavaScript', 'Java', '张三']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"infos_list #查看一下列表"
]
},
{
"cell_type": "code",
"execution_count": 13,
"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[1;32m 1\u001b[0m \u001b[0;31m# 索引不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\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;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m: pop index out of range"
]
}
],
"source": [
"# 索引不存在就报错\n",
"infos_list.pop(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`infos_list.remove(\"张三\")` # `remove(\"\")`删除指定元素,不存在就报错\n",
"\n",
"`del infos_list[1]` # 删除指定下标元素,不存在就报错\n",
"\n",
"`del infos_list` # 删除集合(集合再访问就不存在了)不同于C#给集合赋null\n",
"\n",
"关于`del`的删除后面还会说,这个和`linux`里面的`ln`引用删除类似"
]
},
{
"cell_type": "code",
"execution_count": 14,
"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": 15,
"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": 16,
"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": 17,
"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": 18,
"metadata": {},
"outputs": [],
"source": [
"del infos_list # 删除集合(集合再访问就不存在了)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"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": "markdown",
"metadata": {},
"source": [
"### 1.4.列表修改\n",
"\n",
"Python修改:(**只能通过索引修改**)\n",
"\n",
"`infos_list2[1]=\"PHP\"` # 只有下标修改一种方式,**不存在则异常**\n",
"\n",
"想按值修改需要**先查下标再修改** eg:\n",
"\n",
"`infos_list2.index(\"张三\")`\n",
"\n",
"`infos_list2[0]=\"GO\"`\n",
"\n",
"`infos_list2.index(\"dnt\")` # **不存在则异常**"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['张三', 21]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 修改 xxx[index]=xx\n",
"# 注意:一般不推荐在for循环里面修改\n",
"infos_list2 #查看list2列表"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['张三', 'PHP']\n"
]
}
],
"source": [
"infos_list2[1]=\"PHP\" #只有下标修改一种方式\n",
"print(infos_list2)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"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": 23,
"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": 24,
"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": 25,
"metadata": {},
"outputs": [],
"source": [
"# 知识面拓展: https://www.zhihu.com/question/49098374\n",
"# 为什么python中不建议在for循环中修改列表?\n",
"# 由于在遍历的过程中,删除了其中一个元素,导致后面的元素整体前移,导致有个元素成了漏网之鱼。\n",
"# 同样的,在遍历过程中,使用插入操作,也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。\n",
"# 如果使用while,则可以在面对这样情况的时候灵活应对。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.查询系列\n",
"\n",
"`in, not in, index, count`"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# 查询 in, not in, index, count\n",
"names_list=[\"张三\",\"李四\",\"王二麻子\"]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"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": 28,
"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": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"# 查询王二麻子的索引\n",
"print(names_list.index(\"王二麻子\"))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"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": "markdown",
"metadata": {},
"source": [
"### 1.6.排序系列\n",
"\n",
"`num_list.reverse() `# 倒序\n",
"\n",
"`num_list.sort()` # 从小到大排序\n",
"\n",
"`num_list.sort(reverse=True)` # 从大到小"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"# 排序专用\n",
"num_list=[1,3,5,88,7]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[7, 88, 5, 3, 1]\n"
]
}
],
"source": [
"# 倒序 reverse 逆置\n",
"num_list.reverse()\n",
"print(num_list)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"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": 34,
"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": "markdown",
"metadata": {},
"source": [
"### 1.7.列表切片\n",
"\n",
"列表的切片操作很有用,主要跟数据相关,实际应用中和dict(后面会讲)联合使用\n",
"\n",
"python切片语法:`[start_index:end_index:step]` (**end_index取不到**)\n",
"\n",
"先说说 `range`"
]
},
{
"cell_type": "code",
"execution_count": 35,
"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": 36,
"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": 37,
"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": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 取前10个元素\n",
"top100[:10] #等价于:top100[0:10]"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[91, 92, 93, 94, 95, 96, 97, 98, 99, 100]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 取最后10个元素\n",
"top100[-10:]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 前11~20(eg:第二页)\n",
"top100[10:20]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 取80~90(eg:倒数第二页)\n",
"top100[-20:-10]"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 前20个数,每两个取一个(eg:隔行换样式)\n",
"top100[:20:2]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 所有数每10个取一个(eg:test的时候十里挑一)\n",
"top100[::10]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.8.Python列表相关的扩展\n",
"\n",
"**列表虽然可以存不同类型,一般我们把相同类型的值存列表里面,不同类型存字典里(key,value)**\n",
"\n",
"**列表嵌套**,获取用下标的方式:`num_list[5][1]`"
]
},
{
"cell_type": "code",
"execution_count": 44,
"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": 45,
"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": 46,
"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": "markdown",
"metadata": {},
"source": [
"**补充概念**,`str` 和 `tuple` 也可以用切片操作哦~\n",
"\n",
"str上次说了,这次说下Tuple(后面会继续说Tuple,先了解下吧)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2)"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 取前两个 返回元组\n",
"(1,2,3,4,5)[:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.9.列表生成式\n",
"\n",
"**列表生成式**是Python内置用来 **创建list的生成式**\n",
"\n",
"eg:要生成 list `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`\n",
"\n",
"传统方法是通过循环来实现,比如:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"i=1\n",
"my_list=[]\n",
"while(i<11):\n",
" my_list.append(i)\n",
" i+=1"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"有了列表生成式就方便了 `list(range(1, 11))`(之前说列表切片的时候稍微引入了一下range)\n",
"\n",
"另一种写法:`[x for x in range(1,11)]` 来看看案例: "
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(range(1, 11))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x for x in range(1,11)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可能有人会问,第一种写法不是挺好的嘛,为什么要用第二种复杂写法?\n",
"\n",
"看看下面案例你就知道它的强大了(能简写就简单)\n",
"\n",
"现在有了`range`生成就更方便了,可如果我们需要 **1~10的平方列表**呢?`[1^2,2^2,....10^2]'"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
]
}
],
"source": [
"my_list=[]\n",
"for i in range(1,11):\n",
" my_list.append(i*i)\n",
" i+=1\n",
"print(my_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"但是循环太繁琐,而列表生成式则可以用一行语句代替循环生成上面的list\n",
"\n",
"`[x * x for x in range(1, 11)]` 你可以这样理解==>**就是我们平时的for循环嘛,前面的参数是返回值罢了**"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x*x for x in range(1,11)]"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"# 把一个list中所有的字符串变成小写\n",
"my_list = ['Hello', 'World', 'I', 'Love', 'You']"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hello', 'world', 'i', 'love', 'you']"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x.lower() for x in my_list]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"列表生成式的强大仅限于此嘛?No~\n",
"\n",
"**for循环后面还可以加上if判断** `[x for x in range(1, 11) if x % 2 == 0]`\n",
"\n",
"**多重for循环嵌套** `[x + y for x in 'ABC' for y in 'AB']`"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1~10之间的偶数\n",
"[x for x in range(1, 11) if x % 2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['AA', 'AB', 'BA', 'BB', 'CA', 'CB']"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 数学里面的全排列\n",
"[x + y for x in 'ABC' for y in 'AB']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"其实你可以把他看成\n",
"```py\n",
"list1=[]\n",
"for x in range(1,5):\n",
" for y in range(1,4):\n",
" list1.append((x,y))\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 1),\n",
" (1, 2),\n",
" (1, 3),\n",
" (2, 1),\n",
" (2, 2),\n",
" (2, 3),\n",
" (3, 1),\n",
" (3, 2),\n",
" (3, 3),\n",
" (4, 1),\n",
" (4, 2),\n",
" (4, 3)]"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 数学里面的坐标轴(元组马上就讲了,你可以看看)\n",
"[(x,y) for x in range(1,5) for y in range(1,4)]"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 1, 1),\n",
" (1, 1, 2),\n",
" (1, 2, 1),\n",
" (1, 2, 2),\n",
" (1, 3, 1),\n",
" (1, 3, 2),\n",
" (2, 1, 1),\n",
" (2, 1, 2),\n",
" (2, 2, 1),\n",
" (2, 2, 2),\n",
" (2, 3, 1),\n",
" (2, 3, 2),\n",
" (3, 1, 1),\n",
" (3, 1, 2),\n",
" (3, 2, 1),\n",
" (3, 2, 2),\n",
" (3, 3, 1),\n",
" (3, 3, 2),\n",
" (4, 1, 1),\n",
" (4, 1, 2),\n",
" (4, 2, 1),\n",
" (4, 2, 2),\n",
" (4, 3, 1),\n",
" (4, 3, 2)]"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# (x,y,z)\n",
"[(x,y,z) for x in range(1,5) for y in range(1,4) for z in range(1,3)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.CSharp列表相关\n",
"\n",
"### 2.1.列表定义、遍历\n",
"\n",
"`var infos_list = new List