AI项目博客
记录 AI 工程实践与大模型应用

Python 学习笔记 12|数据容器 · 元组

3. 元组

3.1. 概述

元组:用来存放一组有序的数据,但其中的内容一旦创建就不可修改(不能增、删、改,只能查)。

由于元组不可变,所以元组不能使用append()、insert()这些方法,它里面的元素也不能被重新赋值。

3.2. 定义元组

使用方括号()来定义一个列表,用,去分隔不同的元素:

# 定义有内容的元组
t1 = (28, 67, 21, 67, 11)
t2 = ('北京', '', '你好')
t3 = (100, True, '你好', None)
t4 = (100, True, '你好', None, (50, 60, 70))
print(type(t1), t1)  # <class 'tuple'> (28, 67, 21, 67, 11)
print(type(t2), t2)  # <class 'tuple'> ('北京', '', '你好')
print(type(t3), t3)  # <class 'tuple'> (100, True, '你好', None)
print(type(t4), t4)  # <class 'tuple'> (100, True, '你好', None, (50, 60, 70))

# 定义空元组
t1 = ()
t2 = tuple()
print(type(t1), t1)  # <class 'tuple'> ()
print(type(t2), t2)  # <class 'tuple'> ()

当元组中只有一个元素时,末尾必须写上,

# 定义只有一个元素的元组
t1 = ('你好',)
t2 = (18,)
print(type(t1), t1)  # <class 'tuple'> ('你好',)
print(type(t2), t2)  # <class 'tuple'> (18,)

实际开发中的元组,不一定是我们自己定义的,比如函数的可变参数*args就是一个元组

# 实际开发中的元组,不一定是我们自己定义的,比如函数的可变参数*args就是一个元组
def demo(*args):
    return sum(args)
result = demo(100, 200, 300)
print(result)  # 600

3.3. 读取数据

元组也支持下标,所以使用元组名[索引值]的方式来读取值。

# 元组的下标
t1 = (28, 67, 21, 67, 11)
print(t1[3])  # 67
print(t1[-1]) # 11

3.4. 元组不可修改

元组中的元素,不可修改,但元组中如果存放了可变类型(如:列表),那可变类型中的内容仍可修改。

# 元组中的元素,不可修改
t1 = (28, 67, 21, 67, 11)
t1[0] = 100

# 元组中的元素,不可修改,但元组中如果存放了可变类型(列表),那可变类型中的内容仍可修改
t2 = (28, 67, 21, 67, 11, [100, 200, 300, ('你好', '')])
t2[5] = 400
t2[5][2] = 400
t2[5][3][0] = 'hello'
print(t2)

3.5. 元组的常用方法

由于元组不可修改,所以它的常用方法只有两个:

1️⃣使用元组.index(元素),获取指定元素在元组中第一次出现的下标。

# index 方法:获取指定元素在元组中第一次出现的下标。
t1 = (28, 67, 21, 67, 11)
result = t1.index(67)
print(result)  # 1

2️⃣使用元组.count(元组),统计指定元素在元组中出现的次数。

# count 方法:统计指定元素在元组中出现的次数。
t1 = (28, 67, 21, 67, 11)
result = t1.count(67)
print(result)  # 2

3.6. 元组的常用内置函数

元组的常用内置函数和列表一样,依然是这几个:max、min、len、sorted、sum。

# 常用内置函数
# max 函数,返回元组中的最大值
t1 = (23, 11, 32, 30, 17)
result = max(t1)
print(result)  # 32

# min 函数,返回元组中的最小值
t1 = (23, 11, 32, 30, 17)
result = min(t1)
print(result)  # 11

# len 函数,返回元组中元素的个数(元组长度)
t1 = (23, 11, 32, 30, 17)
result = len(t1)
print(result)  # 5

# sorted 函数,对元组进行排序(不修改原元组,返回一个新的列表)
t1 = (23, 11, 32, 30, 17)
result = sorted(t1, reverse=True)
print(tuple(result)) # (32, 30, 23, 17, 11)

# sum 函数,统计元组中所有元素的和(元素必须是纯数字)
t1 = (23, 11, 32, 30, 17)
result = sum(t1)
print(result) # 113

3.7. 元组的循环遍历

元组的遍历与列表一样,可以使用while循环遍历,或for循环遍历。

# 元组的循环遍历
t1 = (23, 11, 32, 30, 17)

# while循环遍历
index = 0
while index < len(t1):
    print(t1[index])
    index += 1
# 元组的循环遍历
t1 = (23, 11, 32, 30, 17)

# for循环遍历
for item in t1:
    print(item)

3.8. 解包列表或元组传参

解包列表、解包元组传参,就是把其中的元素依次取出,作为多个独立的参数传入函数。

# 定义函数时,使用*args(变量不一定非要用args,比如写:*data也行),将收到的多个参数,打包成一个元组
def test(*args):
    print(f'我是test函数,我收到的参数是:{args},参数类型是:{type(args)}')

list1 = [100, 200, 300, 400]
tuple1 = ('你好', '北京', '')

# 函数调用时,正常传递:列表 或 元组
# test(list1)
# test(tuple1)

# 函数调用时,使用*对:列表 或 元组进行解包后,再传递参数
test(*list1)  # 此种写法相当于:test(100, 200, 300, 400)
test(*tuple1)  # 此种写法相当于:test('你好', '北京', '')

3.9. 元组特点总结

  • 可存放不同类型的元素。
  • 元素是有序存储的(正索引、负索引)。
  • 元组中的元素允许重复。
  • 元素不允许修改文(不能:增、删、改、只能:查)。
  • 长度固定定(一旦创建,元素个数不能增减)。

📍一句话总结:元组是一种“只读”的数据容器,想保存一批“不会变的数据”时,首选元组。

3.10. 元组 VS 列表

区别点 列表(list) 元组(tuple)
是否可变 可变 不可变
使用场景 可变数据集合 不变的结构化数据,安全性更高
语义 表示一组可能变化的数据 表示一组固定结构的数据

📢注意:元组不是用来替代列表的,而是用来在数据不需要修改的情况下,作为列表的补充选择。

4. 字符串

4.1. 概述

字符串(str):用来存放一组有序的字符数据,但其中的内容不可修改(只能查,不能增删改), 我们之前讲解了一部分字符串的相关知识,如:字符串的定义方式、字符串的格式化输出等,这些内容就不在本小节重复讲解了。

4.2. 字符串的特点

1️⃣字符串和列表、元组一样,也支持下标

# 字符串的下标
msg = 'welcome to atguigu'
print(msg[3])  # c
print(msg[-1]) # u

2️⃣字符串不可修改,不可嵌套

# 字符串中的字符,不可修改
msg = 'welcome to atguigu'
msg[0] = 'a'

# 字符串不能嵌套
msg = 'welcome to'hello' atguigu'
msg = 'welcome to"hello" atguigu'
msg = 'welcome to'hello' atguigu'

4.3. 字符串常用方法

1️⃣使用字符串.index(字符),获取『指定字符』在字符串中『第一次』出现的下标,返回值:下标。

# index 方法:获取指定字符,在字符串中第一次出现的下标
msg = 'welcome to atguigu'
result = msg.index('t')
print(result)  # 8

2️⃣使用字符串.split(字符),将字符串按照『指定字符』进行分隔,返回值:列表。

# split 方法:将字符串按照指定字符进行分隔,并将分隔后的内容存入一个列表
msg  = '@atguigu@你好'
result = msg.split('@')
print(msg)  # @atguigu@你好
print(result)  # ['', 'atguigu', '你好']

3️⃣使用字符串.replace(字符串片段),将字符串中的某个字符串片段,替换成目标字符串,不会修改原字符串,返回新字符串。

# replace 方法:将字符串中的某个字符片段,替换成目标字符串(不修改原字符串,返回新字符串)
msg = 'welcome to atguigu'
result = msg.replace('atguigu', '')
print(msg)    # welcome to atguigu
print(result) # welcome to 

4️⃣使用字符串.count(字符),统计『指定字符』在字符串中出现的次数,返回值:下标。

# count 方法:统计指定字符,在字符串中出现的次数
msg = 'welcome to atguigu'
result = msg.count('g')
print(result)  # 2

5️⃣使用字符串.strip(),从某个字符串中删除指定字符串中的任意字符,不会修改原字符串,返回值:新字符串。

# strip 方法:从某个字符串中删除:指定字符串中的任意字符
# 规则:从字符串两端开始删除,直到遇到第一个不在字符串中的字符就停下
msg = '666尚6硅6谷666'
result = msg.strip('6')
print(msg)    # 666尚6硅6谷666
print(result) # 尚6硅6谷 

msg = '1234尚12硅34谷4321'
result = msg.strip('1324')
print(msg)     # 1234尚12硅34谷4321
print(result)  # 尚12硅34谷

msg = '34215尚12硅34谷4132'
result = msg.strip('5432')
print(msg)   # 34215尚12硅34谷4132
print(result)# 15尚12硅34谷41

msg = '    '
result = msg.strip()
print(msg)   #     
print(result)# 

4.4. 字符串常用内置函数

字符串也可以使用:max、min、len、sorted、sum函数,但实际开发中len函数最常用。

# len 函数:统计字符串中字符的个数(字符串长度)
msg = 'welcome to atguigu'
result = len(msg)
print(result) # 18

4.5. 遍历字符串

字符串的遍历,与列表一样,可以使用while循环遍历,或for循环遍历。

msg = 'welcome to atguigu'
# while循环遍历
index = 0
while index < len(msg):
    print(msg[index])
    index += 1
msg = 'welcome to atguigu'

# for循环遍历
for item in msg:
    print(item)
赞(0)
未经允许不得转载:AI项目博客 » Python 学习笔记 12|数据容器 · 元组

评论 抢沙发

AI项目博客

专注 AI 工程化落地 —— 覆盖智能体、大模型、Java AI 与 Python 的学习笔记,以及 AI 平台的项目实战记录。

关于本站文章归档

登录

找回密码

注册