python3 列表、元组操作

>> alist = [1,2,3,4,5,6,7,8,9]
>> alist
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>> alist. #两次TAB键

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'

元组:
atuple = (1, [], 3)

>> atuple = (1, [], 3)
>> atuple
(1, [], 3)
>> atuple[1]
[]>>> atuple[1].append(2)
>> atuple
(1, [2], 3)
>> atuple[1].append(3)
>> atuple
(1, [2, 3], 3)

猜你喜欢

转载自blog.51cto.com/13399699/2128444