Python学习笔记(六)Python组合数据类型
<p> 在之前我们学会了<strong>数字类型</strong>,包括<strong>整数类型、浮点类型和复数类型</strong>,这些类型仅能表示一个数据,这种表示单一数据的类型称为<strong>基本数据类型</strong>。然而,实际计算中却存在大量同时处理多个数据的情况,这种需要将多个数据有效组织起来并统一表示,这种能够表示多个数据的类型称为<strong>组合数据类型</strong>。</p><h2>一、组合数据类型概述</h2>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>组合数据类型</strong></span>更能够将多个同类或不同类型组织起来,通过单一的表示使数据更有序、更容易。根据数据之间的关系,组合数据类型可以分为3类:<strong>序列类型、集合类型和映射类型</strong>。</p>
<p><strong>序列类型</strong>是一个元素向量,元素之间的存在先后关系,通过序号访问,元素之间不排他。</p>
<p><strong>集合类型</strong>是一个元素类型,元素之间无序,相同元素在集合中唯一存在。</p>
<p><strong>映射类型</strong>是“键-值”数据项的组合,每个元素是一个键值对,表示为(key, value)。</p>
<p><img src="https://img2018.cnblogs.com/blog/1785299/201910/1785299-20191024181558953-266615461.jpg"></p>
<p> </p>
<h3>1.序列类型</h3>
<p><strong>序列类型</strong>是一堆元素向量,元素之间存在先后关系,通过序号访问。</p>
<p>Python中有很多数据类型都是序列类型,其中比较重要的是str(字符串)、tuple(元组)list(列表)可以看成是单一字符的有序序列,属于序列类型。</p>
<p>序列类型有12个通用的操作符和函数</p>
<p> </p>
<table style="width: 665px" border="0">
<tbody>
<tr>
<td>操 作 符</td>
<td>描 述</td>
</tr>
<tr>
<td>x in s</td>
<td>如果x是是的元素,返回True,否者返回False</td>
</tr>
<tr>
<td>x not in s</td>
<td>如果x不是s的元素,返回True,否则返回False</td>
</tr>
<tr>
<td>s + t</td>
<td>连接S和t</td>
</tr>
<tr>
<td>s * n 或 n * s</td>
<td>将序列s复制n次</td>
</tr>
<tr>
<td>s</td>
<td>索引,返回序列的第i个元素</td>
</tr>
<tr>
<td>s</td>
<td>分片,返回包含序列s第i到j个元素的子序列(不包含第j个元素)</td>
</tr>
<tr>
<td>s</td>
<td>步骤分片,返回包含序列s第i到j个元素以k为步数的子序列</td>
</tr>
<tr>
<td>len(s)</td>
<td>序列s的元素个数(长度)</td>
</tr>
<tr>
<td>min(s)</td>
<td>序列s中的最小元素</td>
</tr>
<tr>
<td>max(s)</td>
<td>序列s中的最大元素</td>
</tr>
<tr>
<td>s.index(x(,i[,j]))</td>
<td>序列s中从i开始到j位置中第一次出现的元素x的位置</td>
</tr>
<tr>
<td>s.count(x)</td>
<td>序列中出现x的总次数<br><br></td>
</tr>
</tbody>
</table>
<p><strong>元组</strong>(tuple)是序列类型中比较特殊的类型,因为它一旦创建就不能被修改。Python中元组采用逗号和圆括号(可选)来表示,如:</p>
<div class="cnblogs_code">
<pre>>>>creature = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cat</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">dog</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">tiger</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bear</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
print(creature)
</span>>>>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">cat</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">dog</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">tiger</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">bear</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<br><br>>>>creature = "cat", "dog", "tiger", "bear"<br> all=(1,2,456,creature) #元组中包含元组creature<br> print(all)<br> print(all)<br> print(all[-1])<br>>>>(1,<em id="__mceDel"><em id="__mceDel"> 2, 456, ('cat', 'dog', 'tiger',</em></em> 'bear'))<br>>>>456<br>>>>tiger</pre>
</div>
<p>其中,一个元组可以作为另一个元组的元素,可以采用多级索引获取信息,例如上面的元组all中包含了元组creature,可以用all[-1]获取对应元素值。</p>
<p> </p>
<p>元组除了用于表达固定数据外,还常用于以下3种情况:<strong>函数多返回值、多变量同步赋值、循环遍历</strong>,例如:</p>
<div class="cnblogs_code">
<pre>>>><span style="color: rgba(0, 0, 255, 1)">def</span> func(x): <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">函数多返回值 </span>
<span style="color: rgba(0, 0, 255, 1)">return</span> x,x*x; <br><br>>>>a,b = 'dog','cat' #多变量同步赋值<br><br>>>>import math #循环遍历<br> for x,y in ((1,0),(2,5),(3,8)):<br> print(math.hypot(x,y)) #求多个坐标值到原点的距离</pre>
</div>
<p> </p>
<h3>2.集合类型</h3>
<p><strong>集合类型</strong>与数学中集合的概念一致,即包含0个或多个数据项的无序组合。</p>
<p>集合中的元素不可重复,元素类型只能是固定数据类型,例如集合、浮点数、字符串、元组等,列表、字典、和集合类型本身都是可变数据类型,不能作为集合的元素出现。</p>
<p>由于集合是无序组合,它没有索引和位置的概念,不能分片,集合中元素可以动态增加和删除。集合用大括号({})表示,可以用赋值语句生成一个集合,例如:</p>
<div class="cnblogs_code">
<pre>>>>S={100,200,(666,25),<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">YES</span><span style="color: rgba(128, 0, 0, 1)">"</span>,458<span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(S)
</span>>>>{100, 200, 458, 'YES', (666, 25)}</pre>
</div>
<p>从上面可以看出,由于集合的元素是无序的,集合的打印效果与定义顺序可以不一致。</p>
<p>集合类型有10个操作符</p>
<table border="0">
<tbody>
<tr>
<td style="text-align: center">操 作 符</td>
<td style="text-align: center">描 述</td>
</tr>
<tr>
<td>S-T 或 S.difference(T)</td>
<td>返回一个新集合,包括在集合S中但不在集合T中的元素</td>
</tr>
<tr>
<td>S-=T 或 S.difference_update(T)</td>
<td>更新集合S,包括在集合S中但不在集合T中的元素</td>
</tr>
<tr>
<td>S & T 或 S.intersection(T)</td>
<td>返回一个新集合,包括同时在集合S和T中的元素</td>
</tr>
<tr>
<td>S&=T 或 S.intersection_update(T)</td>
<td>更新集合S,包括同时在集合S和T中的元素</td>
</tr>
<tr>
<td>s=^T 或 S.symmetric-difference(T)</td>
<td>返回一个新集合,包括集合S和T中的元素,但不包括同时在其中的元素</td>
</tr>
<tr>
<td>s=^T 或 S.symmetric-difference_update(T)</td>
<td>更新集合S,包括集合S和T中的元素,但不包括同时在其中的元素</td>
</tr>
<tr>
<td>S|T 或 S.union(T)</td>
<td>返回一个新集合,包括集合S和T中的所有元素</td>
</tr>
<tr>
<td>S=|T 或 S.update(T)</td>
<td>更新集合S,包括集合S和T中的所有元素</td>
</tr>
<tr>
<td>S<=T 或 S.update(T)</td>
<td>如果S和T相同或S是T的子集,返回True,否则返回False,可以用S>T判断S是否是T的真子集</td>
</tr>
<tr>
<td>S>=T 或 S.issuperset(T)</td>
<td>如果S和T相同或S是T的超集,返回True,否则返回False,可以用S>T判断S是否是T的真超集</td>
</tr>
</tbody>
</table>
<p>集合类型与其他类型最大的不同在于它不包含重复元素,因此,当需要对一维数据进行去重或进行数据重复处理时,一般通过集合来完成。</p>
<p> </p>
<h3><em id="__mceDel">3.映射类型 </em></h3>
<p><strong>映射类型</strong>是“键-值”数据项的组合,每个元素是一个键值对,即元素是(key,value),元素之间是无序的。</p>
<p>键(key)表示一个属性,也可以理解为一个类别或项目,值(value)是属性的内容,键值对刻画了一个属性和它的值。键值对将映射关系结构化,用于存储和表达。</p>
<p>在Python中,映射类型主要以字典(dict)体现。</p>
<p> </p>
<h2>二、列表类型和操作</h2>
<p>列表是包含0个或多个对象引用的有序序列,没有长度限制,可自由增删元素,使用灵活。</p>
<p>1.列表类型的概念</p>
<p>列表(list)是包含0个或多个对象引用的有序序列,属于序列类型。与元组不同,列表的长度和内容都是可变的,可自由对列表中的数据项进行增加、删除或替换</p>
<p>2.列表类型的操作</p>
<table border="0">
<tbody>
<tr>
<td style="text-align: center">函数或方法</td>
<td style="text-align: center">描述</td>
</tr>
<tr>
<td>ls = x</td>
<td>替换类表 ls 第 i 数据项为 x</td>
</tr>
<tr>
<td>ls =l t</td>
<td>用列表 lt 替换列表 ls 中第 i 到第 j 项数据(不含第 j 项)</td>
</tr>
<tr>
<td>ls = lt</td>
<td>用列表 lt 替换列表 ls 中第 i 到第j项以k为步数的数据(不含第j项)</td>
</tr>
<tr>
<td>del ls</td>
<td>删除列表 ls 第i到第 j 项数据,等价于ls=[]</td>
</tr>
<tr>
<td>del ls</td>
<td>删除列表 ls 第 i 到第 j 项以 k 为步数的数据</td>
</tr>
<tr>
<td>ls += lt 或 ls.extend(lt)</td>
<td>将列表 lt 元素增加到列表 ls 中</td>
</tr>
<tr>
<td>ls *= n</td>
<td>更新列表 ls,其元素重复 n 次</td>
</tr>
<tr>
<td>ls.append(x)</td>
<td>在列表 s 最后增加一个元素 x</td>
</tr>
<tr>
<td>ls.clear()</td>
<td>删除 ls 中的所有元素</td>
</tr>
<tr>
<td>ls.copy()</td>
<td>生成一个新列表,复制 ls 中的所有元素</td>
</tr>
<tr>
<td>ls.insert(i,x)</td>
<td>在列表 ls 的第 i 位置增加元素 x</td>
</tr>
<tr>
<td>ls.pop(i)</td>
<td>将列表 ls 中的第 i 项元素取出并删除该元素</td>
</tr>
<tr>
<td>ls.remove(x)</td>
<td>将列表中出现的第一个元素 x 删除</td>
</tr>
<tr>
<td>ls.reserve(x)</td>
<td>列表 ls 中的元素反转</td>
</tr>
</tbody>
</table>
<p> </p>
<p> </p>
<h2>三、字典类型和操作</h2>
<p>列表是存储和检索数据的有序序列。当访问列表中的元素时,可以通过整数的索引来查找它,这个索引时元素在列表中的符号,列表的索引模式是“<整数序号>查找<被索引内容>”</p>
<p> </p>
<h3>1.字典类型的概念</h3>
<p>通过任意键信息查找一组数据中值信息的过程叫做映射,Python语言中通过字典实现映射。Python语言中的字典可以通过大括号({})建立,建立模式如下:</p>
<div class="cnblogs_code">
<pre>{<键1>:<值1>, <键2>:<值2>, ... ,<键n>:<值n>}</pre>
</div>
<p>其中,键和值通过冒号连接,不同键值对通过逗号隔开。</p>
<p>例如:</p>
<div class="cnblogs_code">
<pre>>>>Dic={<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">中国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">北京</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">美国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">华盛顿</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">法国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">巴黎</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(Dic)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(Dic[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">中国</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">])
</span>>>>{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">中国</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">北京</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">美国</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">华盛顿</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">法国</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">巴黎</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">}
</span>>>>中国</pre>
</div>
<p>一般来说,字典中键值对的访问模式如下,采用中括号格式:</p>
<div class="cnblogs_code">
<pre><值> = <字典变量> [<键>]</pre>
</div>
<p> </p>
<h3>2.字典类型的操作</h3>
<p>与列表类似Python字典也有非常灵活的操作方法。使用大括号可以创建字典,并指定初始值,通过中括号可以增加新的元素,例如:</p>
<div class="cnblogs_code">
<pre>>>>Dic={<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">中国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">北京</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">美国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">华盛顿</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">法国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">巴黎</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">}
Dic{</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">英国</span><span style="color: rgba(128, 0, 0, 1)">"</span>}=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">伦敦</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(Dic)
</span>>>>{<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">中国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">北京</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">美国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">华盛顿</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">法国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">巴黎</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">英国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">伦敦</span><span style="color: rgba(128, 0, 0, 1)">"</span>}</pre>
</div>
<p>注意:直接使用大括号({})生成一个空的字典。生成空集合需要使用函数set()。</p>
<p>字典类型的函数和方法:</p>
<table border="0">
<tbody>
<tr>
<td style="text-align: center">函数和方法</td>
<td style="text-align: center">描 述</td>
</tr>
<tr>
<td><d>.keys()</td>
<td>返回所有的键信息</td>
</tr>
<tr>
<td><d>.values()</td>
<td>返回所有的值信息 </td>
</tr>
<tr>
<td><d>.items()</td>
<td>返回所有的键值对 </td>
</tr>
<tr>
<td><d>.get(<key>,<default>)</td>
<td>键存在则返回相应值,否则返回默认值 </td>
</tr>
<tr>
<td><d>.pop(<key>,<default>)</td>
<td>键存在则返回返回值,同时删除键值对,否则返回默认值 </td>
</tr>
<tr>
<td><d>.popitem()</td>
<td>随机从字典中取出一个键值对,以元组(key,value)形式返回 </td>
</tr>
<tr>
<td><d>.clear() </td>
<td>删除所有的键值对 </td>
</tr>
<tr>
<td> del<d>[<key>] </td>
<td>删除字典中某一个键值对 </td>
</tr>
<tr>
<td> <key>in<d></td>
<td>如果键在字典中返回True,否则返回False </td>
</tr>
</tbody>
</table>
<p>与其他组合类型一样,字典可以通过for-in语句对其元素进行遍历,例如:</p>
<div class="cnblogs_code">
<pre>>>>Dic={<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">中国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">北京</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">美国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">华盛顿</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">法国</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">巴黎</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> Dic:
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(i)
</span>>>><span style="color: rgba(0, 0, 0, 1)">中国
美国
法国</span></pre>
</div>
<p>所以,字典有以下一些基本原则:</p>
<p>(1)字典是一个键值对的集合,该集合以键为索引,一个键信息只对应一个值信息。</p>
<p>(2)字典中元素以键信息为索引访问。</p>
<p>(3)字典长度是可变的,可以通过对键信息赋值实现增加或修改键值对。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/Y-xp/p/11733995.html
頁:
[1]