zoukankan      html  css  js  c++  java
  • python——元组(tuple)基本操作

    元组被称为只读列表,数据可被查询,但不能被修改,类似于列表的切片操作,元组写在小括号里面()元素之前用逗号隔开

    对于一些不想被修改的数据,可以用元组来保存

    #  创建元组

    1)创建空元组

    # 创建空元组
    tup = ( )
    print (tup)
    print (type(tup)) # 使用type函数查看类型
    
    #输出结果
    ()
    <class 'tuple'>

    2)创建元组 (只有一个元素时,在元素后面加上逗号)

    # 创建元组 (只有一个元素时,在元素后面加上逗号)
    tup = (1,)  # 元组中只有一个元素时,在元素后面加上逗号,否则会被当成其他数据类型处理
    print (tup)
    print (type(tup)) # 使用type函数查看类型
    
    #输出结果
    (1,)
    <class 'tuple'>

    3)创建元组(多个元素)

    tup = (1,2,["a","b","c"],"a")
    print (tup)
    
    #输出结果
    (1, 2, ['a', 'b', 'c'], 'a')

    4) # 将列表转化为元组

    list_name = ["python book","Mac","bile","kindle"]
    tup = tuple(list_name) # 将列表转为元组
    print(type(list_name))  # 查看list_name类型,并将结果打印出来
    print(type(tup))    #  查看tup类型,并将结果打印出来
    print (tup) 
    
    # 输出结果
    <class 'list'>
    <class 'tuple'>
    ('python book', 'Mac', 'bile', 'kindle')

    # 查询

    tup = (1, 2, ['a', 'b', 'c'], 'd', 'e', ('gu', 'tang'))
    
    print ("tup[0] =",tup[0]) # 打印索引为0的元素
    #输出结果
    tup[0] = 1
    
    print ("tup[1:] =",tup[1:])   #从索引为1到最后一个元素
    #输出结果
    tup[1:] = (2, ['a', 'b', 'c'], 'd', 'e', ('gu', 'tang'))
    
    print ("tup[:-1] =",tup[:-1])  # 到倒第二个元素但不包含第二个
    #输出结果
    tup[:-1] = (1, 2, ['a', 'b', 'c'], 'd', 'e')
    
    print ("tup[1::1] =",tup[1::1]) # 等价于tup[1:]   从左到右一个个去取,步长为1
    #输出结果
    tup[1::1] = (2, ['a', 'b', 'c'], 'd', 'e', ('gu', 'tang'))
    
    print ("tup[1::2] =",tup[1::2]) #从左到右隔一个去取  步长为2
    #输出结果
    tup[1::2] = (2, 'd', ('gu', 'tang'))
    
    print ("tup[::-1]",tup[::-1])   # 反向输出 步长为1
    #输出结果
    tup[::-1] (('gu', 'tang'), 'e', 'd', ['a', 'b', 'c'], 2, 1)
    
    print ("tup[::-2]",tup[::-2])   # 反向输出 步长为2(隔一个去取))
    #输出结果
    tup[::-2] (('gu', 'tang'), 'd', 2)
    View Code

    # del  删除  (元素对象不支持删除,但是可以删除整个元组变量)

    # del 删除元组中元素
    up = ('tang', 'guo', 'li','xiu')
    del tup[0]  #   元素对象不支持删除
    # print (tup)
    
    #输出结果
    TypeError: 'tuple' object doesn't support item deletion
    tup = ('tang', 'guo', 'li','xiu')
    del tup  #删除
    print (tup) # del tup ,内存已删除,在打印将提示未定义
    
    #输出结果
    NameError: name 'tup' is not defined

    # count 统计元素个数

    # count 统计元素个数
    tup = ('tang', 'guo', 'li','guo').count('guo')
    print (tup)
    
    #输出结果
    2

    index 返回元素的索引位置

    # index 返回元素的索引位置
    tup = ('tang', 'guo', 'li','xiu')
    print (tup.index('li')) # 返回元素'ii'的索引位置
    
    #输出结果
    2

    len  计算元组中元素的个数

    # len   计算元组中元素的个数
    tup = ('tang', 'guo', 'li','xiu')
    print (len(tup))    # 计算元组长度
    
    #输出结果
    4

    注意事项:

    1、当元组中一个元素时,一定要在元素后面加上逗号

    2、元组中的元素是不允许删除的,但可以使用del语句来删除整个元组

    3、元组没有列表中的增、删、改的操作,只有查的操作

  • 相关阅读:
    node.js之Cookie
    jQuery和js之Cookie实现
    StringRedisTemplate操作Redis
    Could not get a resource from the pool 错误解决
    tableLayoutPanel 列宽度设置
    KRBTabControl(中文)Windows选项卡控件
    KRBTabControl
    Deferred content load was not performed. To provide the content, subscribe to the View's QueryControl event
    where(泛型类型约束)
    ExportAsFixedFormat Visio文件另存为其他几种格式的处理
  • 原文地址:https://www.cnblogs.com/xiaoxiamiaichiyu/p/14250749.html
Copyright © 2011-2022 走看看