Python中高级数据存储及应用!

作者: 14e61d025165 | 来源:发表于2019-04-07 15:07 被阅读0次

1. 列表

1.1列表的概念

列表是一种存储大量数据的存储模型。

1.2列表的特点

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812147" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

Python学习群:683380553,有大牛答疑,有资源共享!是一个非常不错的交流基地!欢迎喜欢Python的小伙伴!

1.3 列表的基本语法

创建列表: 变量名 = [ 数据1,数据2,……]

获取列表数据: 变量名[索引]

修改列表数据: 变量名[索引] = 值

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">list1 = [1,2,3,'Tom','Joe','True','None']
print(list1[4]) # 获取列表指定索引的数据
list1[4] = 'Alin' #修改列表中指定索引的数据复制代码
</pre>

1.4 列表常用方法

方法名 功能 参数 返回值 范例 append(data)关键词:追加 在列表的末尾添加数据 data:加入到列表中的数据 None list1 = [1,2,3] list1.append( "Tom" ) insert(idx,data)关键词:插入 在列表的指定位置插入数据,如果索引位置超过列表数据总量,数据将插入到列表末尾 idx:插入数据的索引位置data:加入列表中的数据 None list1 = [1,2,3] list1.insert(0, "Tom" ) extend(model)关键词:追加全部 在列表的末尾添加参数对象中的所有数据 model:保存有数据的存储模型,该模型接受列表、元组、集合类型的对象 None list1 = [1,2,3] list2 = [4,5,6] tuple2 = (7,8,9) set2 = {10,11,12} list1.extend(list2) list1.extend(tuple2) list1.extend(set2) remove(data)关键词:删除 从列表中删除指定的数据,如果数据不存在将报错 data:要从列表中删除的数据 None list1 = [1,2,3, "Tom" ] list1.remove( "Tom" ) pop(idx)关键词:获取删除 从列表中获取并删除指定索引位置上的数据,如果索引值超过列表数据总量将报错 idx:要从列表中获取的数据对应的索引位置 获取到的数据 list1 = [1,2,3, "Tom" ] data = list1.pop(2) clear()关键词:清空 清空列表中的数据 无 None list1 = [1,2,3, "Tom" ] list1.clear() index(data)关键词:查询位置 查询列表中指定数据对应的索引,如果数据不存在将报错 data:在列表中要查询的数据 查询数据第一次出现的索引 list1 = [1,2,3, "Tom" ] idx = list1.index( "Tom" ) count(data)关键词:统计数量 统计列表中指定数据出现的数量 data:在列表中要统计数量的数据 统计数据出现的次数 list1 = [1,2,3, "Tom" ] num = list1.count( "Tom" ) 2. 元组

2.1 元组的概念

元组是一种存储固定数据的存储模型。

2.2 元组的特点

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812156" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

2.3 元组的基本语法

创建元组: 变量名 = (数据1,数据2,……)

获取元组数据: 变量名[索引]

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">tuple1 = (1,2,3,"Tom",True,False)
print(tuple1[4]) # True
print(tuple1)
tuple2 = (100,)
print(tuple2)复制代码
</pre>

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812163" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

2.4 元组常用方法

方法名 功能 参数 返回值 范例 index(data)关键词:查询位置 查询元组中指定数据对应的索引,如果数据不存在将报错 data:在元组中要查询的数据 查询数据第一次出现的索引 tuple1 = [1,2,3, "Tom" ] idx = tuple1.index( "Tom" ) count(data)关键词:统计数量 统计元组中指定数据出现的数量 data:在元组中要统计数量的数据 统计数据出现的次数 tuple1 = [1,2,3, "Tom" ] num =tuple1.count( "Tom" ) 2.5 注意事项

元组中的数据如果是非引用类型数据,不允许修改。

元组中的数据如果是引用类型对象,该对象不允许替换,而对象的属性值可以发生改变。

2.6 元组的特殊应用

自动组包 :将多个数据赋值给一个变量时,多个数据将包装成一个元组,将元组赋值给变量,该过程称为自动组包。

应用场景:设置返回值为多个值。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">return 1,2,3 等同于 return (1,2,3)复制代码
</pre>

应用场景:动态字符串赋值传值方式。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">print(“坐标%d,%d” % (10,20))复制代码
</pre>

自动解包 :将元组赋值给多个变量时,如果元组数据的数量与变量的数量相同,元组将被自动拆分成多个值,并赋值给对应变量,该过程称为自动解包。

应用场景:多个变量接收返回值

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a,b,c = func()等同于 a,b,c = (1,2,3)等同于a,b,c = 1,2,3复制代码
</pre>

3. 集合

3.1 集合的概念

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812172" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

3.2集合的特点

集合 没有索引 的概念。集合中的数据可以进行添加、删除等操作。

3.3 集合的基本语法

创建集合:变量名 = {数据1,数据2,……}

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">set3 = {True,1,2,3,0,False,4,5,1,2,5} # 数值非0表示True#
输出: {0, True, 2, 3, 4, 5} 复制代码
</pre>

3.4 集合常用方法

方法名 功能 参数 返回值 范例 add(data)关键词:添加 在集合中添加数据 data:加入到集合中的数据 None set1 = {1,2,3} set1.append( "Tom" ) remove(data)关键词:删除 从集合中删除指定的数据,如果数据不存在将报错 data:要从集合中删除的数据 None set1 = {1,2,3, "Tom" } set1.remove( "Tom" ) pop()关键词:获取删除 从集合中获取并删除第一个数据 无 获取到的数据 set1 = {1,2,3, "Tom" } data = set1.pop() clear()关键词:清空 清空集合中的数据 无 None set1 = {1,2,3, "Tom" } set1.clear() 4. 字典

4.1 字典的概念

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812182" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

4.2 字典的特点

字典 不具有索引 的概念,字典使用 键key 代替索引,可以通过键操作字典中存储的数据值 value 。字典可以根据键key进行数据的添加、删除、修改、查询操作。

4.3 字典的基本语法

创建字典: 变量名 = {键1:值1,键2:值2,……}

添加数据: 变量名[键] = 值 (字典中没有对应的键)

获取数据: 变量名[键]

修改数据: 变量名[键] = 值 (字典中存在对应的键)

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">dict = {'Name': '张三', 'Age': 17, 'Class': '高三一班'}
print("dict['Name']:%s "% dict['Name']) # 取值
print("dict['Age']:%s "% dict['gender'])
​dict['gender'] = '男' # 增加键值对
dict['Age'] = 18 # 修改键值对,原先的值会被替换
dict.pop('class') # 删除class​dict.clear() # 清空字典复制代码
</pre>

4.4 字典常用方法

方法名 功能 参数 返回值 范例 pop(key)关键词:删除获取 从字典中删除指定键key对应的键值对,如果键key不存在将报错 key:要删除的值value对应的键key 被删除的值value dict1 = { "name" : "Tom" , "age" :11} v = dict1.pop( "name" ) popitem()关键词:删除 从字典中删除指定键key对应的键值对,如果键key不存在将报错 key:要删除的键值对对应的键key,实际测试里面不能带参数 被删除的键值对,以元组的形式返回 dict1 = { "name" : "Tom" , "age" :11} v = dict1.popitem() clear()关键词:清空 清空字典中的数据 无 None dict1 = { "name" : "Tom" , "age" :11} dict1.clear() setdefault(key,value)关键词:检测添加 添加新的键值对,如果存在对应的键,则忽略该操作 key:要添加的新键值对对应的键keyvalue: 要添加的新键值对对应的值value 字典中key对应的值,如果是添加则返回参数value,如果不是添加,返回原始key对应的value dict1 = { "name" : "Tom" , "age" :11} dict1.setdefault( "age" ,22) update(dict)关键词:更新数据 使用新字典中的数据对原始字典数据进行更新 dict:新字典对象 None dict1 = { "name" : "Tom" , "age" :11} dict2 = { "address" : "北京" , "age" :22} dict1.update(dict2) get(key)关键词:获取 根据键key查询字典中对应的值,如果键key不存在将返回None key:要查询的键key 根据键key在字典中查询的值value dict1 = { "name" : "Tom" , "age" :11} v = dict1.get( "age" ) keys()关键词:获取键列表 获取字典中所有的键key组成的列表数据 无 由所有键组成的列表 dict1 = { "name" : "Tom" , "age" :11} dict1.keys() values()关键词:获取值列表 获取字典中所有的值value组成的列表数据 无 由所有值组成的列表 dict1 = { "name" : "Tom" , "age" :11} dict1.values() items()关键词:获取键值对列表 获取字典中所有的键值对列表数据 无 由键值对组成的列表,键值对的格式是元组数据 dict1 = { "name" : "Tom" , "age" :11} dict1.items() 4.5 注意事项

字典中的 键是唯一的 。

4.6 字典的作用

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812209" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

5. 格式转换

转换成列表 转换成元组 转换成集合 列表list — tuple (列表对象) set (列表对象) 元组tuple list (元组对象) — set (元组对象) 集合set list (集合对象) tuple (集合对象) — 6. for循环

6.1 for循环作用

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812217" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

使用格式:for变量名 in 列表:

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">list1 = [1, 2, 3, 'Tom']
for data in list1:
print(data, end=' ')
# 输出:1 2 3 Tom 复制代码
</pre>

7. Range

7.1 range的功能

创建连续的整数。

7.2 range的基本语法

格式1:range(m) 生成 0 到 m-1 的整数

格式2:range(m,n) 生成 m 到 n-1 的整数

格式3:range(m,n,s) 生成 m 到 n-1 的整数,整数间隔为s

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">r1 = range(1,5) # 由1到5,不包含5
print(list(r1))r2 = range(1,10,2) # 由1到10,不包含10,每次自增2
print(list(r2))复制代码
</pre>

7.3 range的应用场景

1.配合for循环构造指定次数的循环。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for _ in range(..):​
循环执行的代码​复制代码
</pre>

2.快速创建由连续的整数作为数据的列表、元组、集合对象。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">list(range(..))
tuple(range(..))
set(range(..))​复制代码
</pre>

8. 数据存储结构嵌套

8.1 数据存储结构嵌套概念

数据结构嵌套指一种数据结构中包含的数据是另一种数据结构。

8.2 范例

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">list1 = [1,2,3]
list2 = [4,5,6,7]
list3 = ["Joe","Sam"]
tuple1 = (5,4,3)
set1 = {8,7,6}
list4 = [list1,list2,list3,tuple1,set1]
print(list4)​

输出 [[1, 2, 3], [4, 5, 6, 7], ['Joe', 'Sam'], (5, 4, 3), {8, 6, 7}]复制代码

</pre>

9. 公共方法

方法名 功能 参数 返回值 范例 len(model)关键词:数据总量 获取容器模型中的数据总量 model:保存有数据的存储模型,该模型接受各种容器 容器模型中数据的总量 list1 = [1,2,3, 'Tom' , "valley" ] length = len(list1) max(model)关键词:最大值 获取容器模型中的最大值,对于字典获取字典的键key的最大值 model:保存有数据的存储模型,该模型接受各种容器 容器模型中数据的最大值 list1 = [1,2,3,4,5] max_value = max(list1) min(model)关键词:最小值 获取容器模型中的最小值,对于字典获取字典的键key的最小值 model:保存有数据的存储模型,该模型接受各种容器 容器模型中数据的最小值 list1 = [1,2,3,4,5] min_value = min(list1) 10. 切片

10.1 切片的作用

获取列表、元组或字符串中的局部数据。

10.2 切片基本语法

容器对象[开始索引:结束索引:步长]。

10.3 特殊格式

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">省略开始索引:默认开始索引为0​
范例:list1[:5:1] 等同于 list1[0:5:1]
省略结束索引:默认结束索引为数据总量
​范例:list1[0::1] 等同于 list1[0:len(list1):1]​
省略步长:每次递增索引数为1​ 范例:list1[0:5:] 等同于 list1[0:5:1] ​
负数步长: 反向操作/反向步长操作,需要开始索引、结束索引逆序输入​
范例:list1[5:0:-1]
常见格式:​范例:list1[:5:] 获取索引5之前的数据(不含5)
范例:list1[4::] 获取索引4之后的数据(含4)​复制代码
</pre>

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812230" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

11. 通用运算符

运算符 功能 格式 适用范围 + 将两个容器数据合并放入第一个容器 list1 + list2 列表之间或元组之间(列表与元组之间报错) * 将容器数据复制n次放入容器中 list1 ***** n 列表、元组 in 判断容器中是否包含数据 data in list1 列表、元组、集合、字典(字典判断数据是否在keys()中) not in 判断容器中是否不包含数据 data not in list1 列表、元组、集合、字典(字典判断数据是否不在keys()中) >、>=、==、<=、< 比较两个容器中的数据关系 list1 <= list2 列表、元组、集合

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554620812233" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

12. for…else

12.1for…else基本语法

for 变量名 in 列表:

变量相关操作

else:

循环正常运行结束后执行的操作

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for data in range(5):
print(data,end=' ')
else:
print('结束')​

输出: 0 1 2 3 4 结束复制代码

</pre>

如果中间遇见break语句,循环未执行完毕,不会走else里面的代码片段。

12.2 注意事项

1.如果for循环中执行了break语句,则else中的代码将不执行。

2.while循环同样具有while…else语法格式。

13. 推导式

13.1 推导式基本语法

基础语法格式:循环变量 for循环

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">list1 = [data for data in range(5)]复制代码
</pre>

数据处理语法格式:表达式 for循环

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">list1 = [data*5 for data in range(5)]复制代码
</pre>

数据过滤语法格式: 表达式 for循环 if判断

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">list1 = [data for data in range(5) if data > 200] 复制代码
</pre>

13.2 推导式的作用

推导式可以快速生成数据存储结构中的数据。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 创建包含1到100所有整数的列表
list1 = [data for data in range(1,101)]

创建包含1到10的平方和的列表

list2 = [data**2 for data in range(1,11)]复制代码
</pre>

推导式的局限,不能打印复杂的循环结构数据。

相关文章

网友评论

    本文标题:Python中高级数据存储及应用!

    本文链接:https://www.haomeiwen.com/subject/uljsiqtx.html