常见数据结构-tuple元组

2022-08-19 15:11:39 浏览数 (1)

一、概述

本质

有序集合

特性

与列表非常相似

一旦初始化就不能修改

使用小括号

二、基本使用

创建

代码语言:javascript复制
<span class="hljs-string">'''
创建格式:
    元组名 = (元素1, 元素2, ……, 元素n)
'''</span>
<span class="hljs-comment"># 创建空元祖</span>
t1 = ()
print(t1, type(t1))
<span class="hljs-comment"># 创建带元素的元组,元组的元素类型可以不同</span>
t2 = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>)
print(t2)
<span class="hljs-comment"># 创建含有一个元素的元组,需要加一个逗号</span>
t3 = (<span class="hljs-number">1</span>,)
print(t3, type(t3))

元组元素的访问

代码语言:javascript复制
<span class="hljs-comment"># 取值  元组名[下标]</span>
t4 = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>)
print(t4[<span class="hljs-number">2</span>])
<span class="hljs-comment"># print(t4[7]) #下标越界</span>
print(t4[<span class="hljs-number">-1</span>])
<span class="hljs-comment"># print(t4[-7]) #下标越界</span>
<span class="hljs-comment"># 注意:元素是不能修改的,但是如果元组的元素是列表类型,那么列表中元素是可以修改的</span>
t5 = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,[<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>])
<span class="hljs-comment"># t5[3] = 100 # 报错</span>
<span class="hljs-comment"># t5[5] = [1,2,3] # 报错</span>
t5[<span class="hljs-number">5</span>][<span class="hljs-number">0</span>] = <span class="hljs-number">60</span>
print(t5)

元组操作

代码语言:javascript复制
t6 = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)
t7 = (<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>)
t8 = t6   t7
print(t8, t6, t7)
print(t6 * <span class="hljs-number">3</span>)

元组截取

代码语言:javascript复制
t9 = (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>,<span class="hljs-number">0</span>)
print(t9[<span class="hljs-number">3</span>:<span class="hljs-number">7</span>])
print(t9[<span class="hljs-number">3</span>:])
print(t9[:<span class="hljs-number">7</span>])
print(t9[<span class="hljs-number">3</span>:<span class="hljs-number">-2</span>])

元组对称赋值

代码语言:javascript复制
<span class="hljs-comment"># 用于函数返回多个返回值</span>
num1, num2 = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
<span class="hljs-comment"># 如果只有一个占位符,可以省略小括号,但是最好不要省略</span>
print(<span class="hljs-string">"num1 = %d"</span>%num1)
print(<span class="hljs-string">"num2 = %d"</span>%(num2))

三、操作方法

三、操作方法

  • len(seq)
代码语言:javascript复制
print(len((<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>)))
  • max()
  • min()
  • tuple(seq)

将其他类型的集合转为元组类型

代码语言:javascript复制
print(tuple(<span class="hljs-string">"baism"</span>))
print(tuple([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]))

0 人点赞