python data type (tuple)

Today, I share with you a wave of python data types-tuples, and I found that there are a lot of operations on tuples.

Code 1, as shown below:

#enconding='utf-8'
#Author:Mr.Pan_学狂
#finish_time:2021/3/21
#元组的使用
def tuple1():
    tup = ("8+4**2","0+{}".format(9+int("%d"%(20))),"3*2+%s"%(5))
    #比较字符串的最大值,就是比较字符串的长度。
    #0+{}%d其实是长度为4的字符串,3*2+%s其实是长度为5的字符串

    for i in range(0,len(tup)):
        print('元组的第{}'.format(i+1)+"元素值是%d"%(len(tup[i])))
        print('元组的第{}'.format(i+1)+"元素类型是%s"%(type(tup[i])))
    ls = []#定义一个空列表用于存储元素
    max1 = len(max(tup))#比较元组中最大值/最大长度的字符串
    print('元组未处理前最大值是:{}'.format(max1))
    print("处理前的元组:",tup)#打印元组
    print()#换行

    Length = len(tup)#获取元组的长度
    while True:
        temp = eval(tup[::][Length-1])#eval将字符串的类型还原,逆序索引元组的全部元素
    # temp2 = eval(tup[:2][0])
        ls.append(temp)#添加元素到列表中
        Length -= 1#每次索引减1
        if Length == 0:#当索引等于0时跳出循环
            break
    # ls.append(temp2)
    tup2 = tuple(sorted(ls))#对列表进行排序,再转换成元组类型
    for x in range(0,len(tup2)):
        print('元组的第{}'.format(x+1)+"元素值是%d"%(tup2[x]))#因为eval函数转变成int整形后,没有字符串长度,所以,直接索引元素值。
        print('元组的第{}'.format(x+1)+"元素类型是%s"%(type(tup2[x])))
    max2 = max(tup2)
    print('元组处理后最大值是:'+str(max2))

    print('处理后的元组:',tup2)#打印元组
    ls2 = [i for i in tup2]#列表表达式,将元组类型转换成列表类型。同list(tup2)
    print('转换成列表:',ls2)
tuple1()

Operation result:
Insert picture description here
Code 2:

#元组的操作
def tuple2():
    ls = [1,'2',{
    
    3},{
    
    'num':4},(1,2,),(3,4),(5),[1,2,3],6.5,True & False]
    for l in ls:
        print(l,type(l))#输出列表中各个元素及其对应的数据类型
    print()#换行
    data = ls[5]#虽然结束没有加上逗号,但是多于一个元素且以逗号分隔,默认是tuple元组类型
    data2 = ls[6]#因为没有加逗号,在只有一个元素的时候会默认是计算的小括号,所以是int整形
    print(data,type(data))
    print(data2,type(data2))
    print()#换行
    tup = tuple(ls)
    print('tup:',tup)#将列表转换成元组。
    if True in tup:
        print('right')#打印输出right,其实是判断boolean类型(布尔类型)是否存在元组tup中。
    else:
        print('wrong')
    print()
    tup3 = tup[:1]+tuple([int(tup[1])])+tup[2::]#直接将元组中第二个元素转换成int整形,
    #再变换成list列表,再变换成元组,再拼接。
    tup2 = tup[:1] +tuple([eval(tup[1])])+tup[2::]#元组可以通过切片的方式修改其中的元素,
    # 不过操作很繁琐,通常不建议修改,而且定义元组通常是出于不修改,增加,不删除的目的。
    #因为元组中的第二个元素是字符串'2',要修改成原来的整形需要用索引的方式取出来,
    #然后用eval函数处理字符串表达式转换成int整形再变换成列表的形式,最后变换成元组,
    #再进行元组的拼接,形成新的元组
    print('tup2:',tup2)
    print()
    print('tup3:',tup3)
    if tup2 == tup3:
        print('Right')
    else:
        print('Wrong')
    print()
    #对于元组中单个字符或者是数字,修改要进行多次变换,
    #但是对于元组中其它的数据结构操作却不需要进行多次变换
    tup2[2].add(tup2[6])#让元组的第3个元素(集合)增加元组中第7个元素(int)数字5.
    tup2[3]['num2'] = 5#让元组中第4个元素增加新的键值对
    tup4 = tup2#将变换后的元组赋值给新的元组tup4
    print('tup2:',tup2)
    print(len(tup2))
    print()
    element = tup4[4]+tup4[5]#要修改元组的元素只能使用切片
    tup4 = tup4[:4]+element+tup4[6:]#将元组中的第4个元素和第5个元素合并。
    print('tup4:',tup4)#因为没有严格的额逗号限制。所以,合并时默认是int类型。通过比较tup2和tup4的长度可以看出。
    print(len(tup4))
    #所以,如果要让合并的结果成为一个元组,应该是如下的操作。
    print()
    tup5 = tup4[:4] + tuple([element]) + tup4[8:]
    print('tup5:',tup5)
tuple2()

Operation result:
Insert picture description here
The explanation about the code is written in the comment. Friends who don't understand can communicate with me in private messages.
Finally, thank you all for coming to watch my article. There may be many improprieties in the article, and I hope to point out He Haihan.

Guess you like

Origin blog.csdn.net/weixin_43408020/article/details/115047155