• [Python] 元组操作及方法总结


    元组是有序且不可更改的集合

    1.创建元组

    使用圆括号()直接创建元组

    1. thistuple = ("apple", "banana", "cherry")
    2. # ('apple', 'banana', 'cherry')
    3. print(thistuple)

    使用tuple()构造函数创建元组 

    1. aList = [1,2,3,4]
    2. print(tuple(aList)) # (1, 2, 3, 4)
    3. Astr = "Hello"
    4. print(tuple(Astr)) # ('H', 'e', 'l', 'l', 'o')
    5. # 针对字典,会返回字典的key组成的tuple
    6. aDict = {'name':'Andy','age':18}
    7. print(tuple(aDict)) # ('name', 'age')
    8. # 针对元组,会返回元组自身
    9. aTuple = (1,2,3,4,5)
    10. print(tuple(aTuple)) # (1, 2, 3, 4, 5)
    11. # 将区间转换成元组
    12. range1 = range(1, 6)
    13. print(tuple(range1)) # (1, 2, 3, 4, 5)

    从存储内容上看,元组可以存储整数、实数、字符串、列表、元组等任何类型的数据,并且在同一个元组中,元素的类型可以不同,例如:

    1. aTuple = ("https://www.csdn.net", 1, [2,'a'], ("abc",3.0))
    2. # ('https://www.csdn.net', 1, [2, 'a'], ('abc', 3.0))
    3. print(aTuple)

    定义一个空的元组

    1. test1 = ()
    2. print(test1) # ()
    3. test2 = tuple()
    4. print(test2) # ()

    定义只有一个元素的元组

    1. # 注意test1不是元组,test2是元组
    2. test1 = (1)
    3. type(test1) # int
    4. test2 = (1,)
    5. print(test2) # (1,)
    6. type(test2) # tuple
    7. aList = [1]
    8. print(tuple(aList)) # (1,)

    Python的元组与列表类似,不同之处在于元组的元素不能修改,元组tuple一旦初始化就不能修改

    元组也可以看做是不可变的列表,通常情况下,元组用于保存无需修改的内容

    1. # 无法向元组添加元素
    2. thistuple = ("apple", "banana", "cherry")
    3. # TypeError: 'tuple' object does not support item assignment
    4. thistuple[3] = "orange" # 引发错误
    5. print(thistuple)

    2.元组常见操作方法

    2.1索引

    元组中的所有元素都是有编号的,即我们所说的索引值,从0开始递增

    我们可以使用索引来获取元组中的元素

    1. thistuple = ("Andy", "Odin", "Summer", "Lee", "Jack", "Harry", "Rita")
    2. print(thistuple[0]) # Andy
    3. print(thistuple[2]) # Summer
    4. print(thistuple[4]) # Jack
    5. print(thistuple[-1]) # Rita

    2.2切片 

    除了使用索引访问单个元素外,我们还可以使用切片(slicing)来访问特定范围内的元素

    1. thistuple = ("Andy", "Odin", "Summer", "Lee", "Jack", "Harry", "Rita")
    2. print(thistuple[2:5]) # ('Summer', 'Lee', 'Jack')
    3. print(thistuple[-4:-1]) # ('Lee', 'Jack', 'Harry')

    扩展补充资料

    Python切片操作https://blog.csdn.net/Hudas/article/details/125905111

    2.3计算元组元素个数

    len(tuple)可用于获取元组长度

    注明:tuple代表元组

    1. thistuple = ("Andy", "Odin", "Summer")
    2. print(len(thistuple)) # 3

    2.4判断元素是否存在于元组中 

    运算符in可用于检查特定的值是否包含在元组tuple中,包含在内则返回True,不包含在内则返回False

    1. thistuple = ("Andy", "Odin", "Summer")
    2. print('Andy' in thistuple) # True
    3. print('Rita' in thistuple) # False

    扩展:not in判断指定元素是否不在元组tuple中

    1. thistuple = ("Andy", "Odin", "Summer")
    2. print('Andy' not in thistuple) # False
    3. print('Rita' not in thistuple) # True

    2.5合并元组 

    如需要连接两个或多个元组,可以使用+运算符

    1. tuple1 = ('a', 'b' , 'c')
    2. tuple2 = (1, 2, 3)
    3. tuple3 = tuple1 + tuple2
    4. # ('a', 'b', 'c', 1, 2, 3)
    5. print(tuple3)

    2.6统计某个元素在元组中出现的次数 

    count()用于返回元组中指定值出现的次数 

    1. thistuple = (1, 3, 7, 8, 7, 5, 4, 5, 8, 5)
    2. num = thistuple.count(5)
    3. print(num) # 3

    2.7查找元组指定值第一次出现的索引 

    index()用于在元组中搜索指定的值并返回它被找到的位置

    注意:返回的是查找指定值第一次出现的位置(索引值),如果未找到该值,index()将引发异常

    语法格式

    tuple.index(value)
    1. thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
    2. res = thistuple.index(8)
    3. print(res) # 3

    2.8删除元组

    我们无法删除元组中的元素,但可以完全删除元组

    del关键字可以完全删除元组

    1. thistuple = ("apple", "banana", "cherry")
    2. del thistuple
    3. # 这会引发错误,因为元组已不存在
    4. # NameError: name 'thistuple' is not defined
    5. print(thistuple)

    2.9更改元组值

    创建元组后,我们将无法更改其值(元组是不可变的,或者也称为恒定的),但是有一种解决方法可以进行更改元组值

    思路:首先可以将元组先转换为列表,然后更改列表中的值,最后将列表转换回元组

    1. fruits = ("apple", "banana", "cherry")
    2. list_fruits = list(fruits)
    3. list_fruits[1] = "kiwi"
    4. fruits = tuple(list_fruits)
    5. print(fruits) # ('apple', 'kiwi', 'cherry')

    2.10重复元组

    1. # (1, 1, 1, 1, 1)
    2. print((1,) * 5)
    3. # (1, 2, 1, 2, 1, 2)
    4. print((1,2) * 3)

    2.11元组的最大值/最小值 

    max(tuple)用于返回元组中元素最大值

    min(tuple)用于返回元组中元素最小值

    1. nums = (1,3,5,7,9)
    2. print(max(nums)) # 9
    3. print(min(nums)) # 1
  • 相关阅读:
    强化学习总结2 动态规划
    国际期货投机有什么风险?目前正大怎么样?
    管理好自己,才是一个人最大的本事
    vue 引用百度地图
    Servlet生命周期
    电商前台项目(二):完成Home首页模块业务
    基于.net的应用开发技术-作业一
    SpringCloudAlibaba【二】整合Nacos
    docker 快速上手
    java毕业设计商场会员管理系统源码+lw文档+mybatis+系统+mysql数据库+调试
  • 原文地址:https://blog.csdn.net/Hudas/article/details/126836382