zoukankan      html  css  js  c++  java
  • python3.7[列表] 索引切片

    python3.7[列表]

    索引  切片 排序

     

     

    ####

    列表.sort 永久排序

     

    sorted(列表) 临时排序

     

    ###

    >>> print(sorted(a))
    ['abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
    >>> a
    ['tttttt', 'fff', 'f', 'f', 'f', 'f', 'btte', 'bbb', 'asdf', 'aff', 'abc']
    >>> a.sort(reverse=True)
    >>> a
    ['tttttt', 'fff', 'f', 'f', 'f', 'f', 'btte', 'bbb', 'asdf', 'aff', 'abc']
    >>> a.sort()
    >>> a
    ['abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
    >>>

     

     

    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
    >>> a.reverse()
    >>> a
    ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'f', 'f', 'tttttt', 'fff', 'f', 'f', 'btte', 'bbb', 'asdf', 'aff', 'abc', '666']
    >>>

     

    ##############

    列表 增加元素

    ##########

    >>> a
    ['abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
    >>> a.insert(0,'666')
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
    >>> a.append ('888')
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt', '888']
    >>> a.pop()
    '888'
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']

     ############

    删除

    remove(元素)

    pop

     

    #############

    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'fff', 'tttttt']
    >>> a.append('f')
    >>> a.append('f')
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'fff', 'tttttt', 'f', 'f']
    >>> a.remove('f')
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f']
    >>>

     

    #####

     

    >>> a.extend('aa')
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f', 'a', 'a']
    >>> a.extend('aaaaaa')
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
    >>>

     

     

    ######

    >>> a.index('f')
    6
    >>> a.count('f')
    4
    >>> a
    ['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f']
    >>> len(a)
    12
    >>>

    #########

    切片

    #######

    >>> a[1:4:2]
    ['a', 'a']
    >>> a[1::2]
    ['a', 'a', 'a', 'a', 'f', 'fff', 'f', 'bbb', 'aff', '666']
    >>> a[5:2:-2]
    ['a', 'a']

     ###@@@@

    >>> a
    ['df', 'asdf', 'asd', 'eee', 'as', 'qq']
    >>> a.pop('eee')
    Traceback (most recent call last):
    File "<pyshell#19>", line 1, in <module>
    a.pop('eee')
    TypeError: 'str' object cannot be interpreted as an integer
    >>> a.pop(1)
    'asdf'
    >>> a
    ['df', 'asd', 'eee', 'as', 'qq']
    >>>

    ##########

    >>> a[0:4:2]
    ['df', 'eee']
    >>>

    >>> a.index('eee')
    2

    >>> print(a[5][2].replace('兰','kk'))
    kk
    >>> a
    ['df', 'asd', 'eee', 'as', 'qq', '武藤兰']
    >>> a[0].upper()
    'DF'
    >>> a
    ['df', 'asd', 'eee', 'as', 'qq', '武藤兰']
    >>>

    >>> a.index('df')
    0

    增加1个字符 不增加

    >>> a.extend("ggg")
    >>> a
    ['df', 'asd', 'eee', 'as', 'qq', '武藤兰', 'g', 'g', 'g']
    >>>

  • 相关阅读:
    mysql关联查询
    MySQL数据库面试题(转发来自https://thinkwon.blog.csdn.net/article/details/104778621)
    iview + vue + thinphp5.1 源码
    <!--标签嵌套规则-->
    PHP的基本变量检测方法
    PHP的八种变量
    php变量命名规范
    C++11新特性-常用
    算法设计-DP常见问题及解题技巧
    Web开发-概述
  • 原文地址:https://www.cnblogs.com/xuanbjut/p/11147312.html
Copyright © 2011-2022 走看看