首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3 列表、元组操作

python3 列表、元组操作

作者头像
py3study
发布2020-01-03 16:20:41
4200
发布2020-01-03 16:20:41
举报
文章被收录于专栏:python3python3

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)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档