zoukankan      html  css  js  c++  java
  • 元组

    #元组

    #什么是元组?
    # 元组就是一个不可变的列表,和列表类型比,只不过[]换成了()
    # 在()内用逗号分隔开多个任意类型的值

    #元组的基本使用
    # 用于存放多个值,当存放的多个值只有读的需求没有改的需求时用元组最合适

    # t = (1,3.1,'aaa',(1,2,3),['a','b']) # t = tuple(...)
    #
    # res0 = tuple('hello')
    # print(res0)
    #('h', 'e', 'l', 'l', 'o')

    # res1 = tuple({'x':1,'y':2})
    # print(res1)
    #('x', 'y') 字典key不可变类型

    #常用操作+内置方法
    #优先掌握的操作:

    #按索引取值(正向取+反向取):只能取
    #t = ('a','b',1)
    #t[0]=111 #'tuple' object does not support item assignment赋值

    #切片(顾头不顾尾,步长)
    # t=('h','e','l','l','o')
    # res = t[1:3]
    # print(res) #('e', 'l')

    #长度
    # a = 'hello'
    # t = tuple(a) # ('h', 'e', 'l', 'l', 'o')
    # print(t)
    # print(len(t)) # 5

    #成员运算in和not in
    # a = 'hello'
    # t = tuple(a)
    # print('h' in t) #True

    #循环
    # a = 'hello'
    # t = tuple(a)
    # for item in t:
    # print(item)

    #该类型总结
    #存多个值
    #有序
    #不可变 (值变,id就变,不可变==可hash))
    # t = (1,'a',['x','y','z'])
    # print(id(t[2]))
    # print(id(t))
    # t[2][0]='X'
    # print(t) #(1, 'a', ['X', 'y', 'z'])
    # print(id(t))
    # print(id(t[2]))

    # # 掌握的方法
    t=('c','b','a')
    print(t.index('a')) #index 元素在元组中的位置
    # t.index('xxx')
    # print(t.count('a')) #a 在元组中的个数
  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/OutOfControl/p/9672066.html
Copyright © 2011-2022 走看看