天天看點

python3 清單、元組操作

alist.append(    #添加

alist.count(9)      #統計單個字元出現的次數

alist.insert(   #插入在指定位置插入參數   
        >>> alist.insert(3,4)
        >>> alist
        [1, 2, 3, 4]

alist.reverse() #倒序列印

alist.clear(   

alist.extend('new')       #把new當成三個字元,進行添加
        >>> alist.extend('new')
        >>> alist
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 'n', 'e', 'w']
        >>> alist.extend(['hello','world'])
        >>> alist
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 'n', 'e', 'w', 'hello', 'world']

alist.pop(        #删除,并彈出

alist.sort()    #升序排序,改變清單本身
    >>> alist = [3,5,2,6,2]
    >>> alist.sort()
    >>> alist
    [2, 2, 3, 5, 6]

alist.copy(    

alist.index(6)   #傳回參數的下标,一個參數出現多次傳回第一個參數的下标
    >>> alist.index(6)
        5

alist.remove(   

shuffle    #打亂清單順序
    >>> from random import shuffle
    >>> alist
    [97, 97, 97, 94, 79, 70, 57, 16, 11, 9]
    >>> shuffle(alist)
    >>> alist
    [79, 16, 97, 94, 97, 11, 97, 70, 9, 57]

join:
    >>> str_list = ['h','e','l','l','o']
    >>> str_list
    ['h', 'e', 'l', 'l', 'o']
    >>> ''.join(str_list)         #将清單轉換成字元串,以空為連結符
    'hello'
    >>> '.'.join(str_list)        #以.為連結符,轉換清單為字元串
    'h.e.l.l.o'           

繼續閱讀