• python语法小应用---列表和元组


    声明:本文章为参考总结CSDN上知识点所获,只是用来总结自己学习而用,如有侵权,会删除!

    列表(list):

    列表就像一个线性容器,但是比C++的 lis t扩展多得多

    列表里的元素可以是相同类型,也可以包含各种类型,比如列表里嵌套另一个列表

    [python] view plain copy
     
     print?
    1. >>> L1 = [1,2,3]  
    2. >>> type(L1)  
    3. <class 'list'>  
    4. >>> L1 = [1,'a',2,1.4]  
    5. >>> L1  
    6. [1, 'a', 2, 1.4]  
    7. >>> L1 = [ ['sub'],1,'n']  
    8. >>> L1  
    9. [['sub'], 1, 'n']  

    list的索引是也是从0开始,但也可以从后访问,L1[-1] 表示L1中的最后一个元素

    [python] view plain copy
     
     print?
    1. >>> L1  
    2. [['sub'], 1, 'n']  
    3. >>> L1[0]  
    4. ['sub']  
    5. >>> L1[-1]  
    6. 'n'  


    对列表可以进行切片,切片的操作类似于对函数的调用,返回值一个新的列表

    切片 L1[ x : y : z ] 是半开闭区间(z通常不用写),如L1[1:3] 返回的是一个从 L1[1] 开始到 L1[2] 结束的列表,不包含L1[3]

    x 不写表示从头开始,y 不写表示直到列表结束,z 用于表示步长, 默认是1, 可以认为是在这个区间里每 z 个元素取一个(取第一个),可以是负数,表示从后到前遍历

    [python] view plain copy
     
     print?
    1. >>> L1 = [1,2,3,4,5,6]  
    2. >>> L1[1:3]  
    3. [2, 3]  
    4. >>> L1[:3]  
    5. [1, 2, 3]  
    6. >>> L1[1:]  
    7. [2, 3, 4, 5, 6]  
    8. >>> L1[-3:-1]  
    9. [4, 5]  
    10. >>> L2 = L1[:]  
    11. >>> L2  
    12. [1, 2, 3, 4, 5, 6]  
    13. >>> L1[::2]  
    14. [1, 3, 5]  
    15. >>> L1[::-1]  
    16. [6, 5, 4, 3, 2, 1]  



    列表可以做加法,做乘法,字符串也可以看做一个字符的列表

    [python] view plain copy
     
     print?
    1. >>> L1 = [1,2]  
    2. >>> L2 = [3,4]  
    3. >>> L1 + L2  
    4. [1, 2, 3, 4]  
    5. >>> 5 * L1  
    6. [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]  


    in语句,判断一个对象是否在一个字符串/列表/元组里

    not 语句表示对后面的否定

    len  可以检测字符串/列表/元祖/字典的元素个数

    max 可以返回最大元素,min 返回最小元素

    [python] view plain copy
     
     print?
    1. >>> L1  
    2. [1, 2, 3, 4, 2]  
    3. >>> in L1  
    4. True  
    5. >>> in L1  
    6. False  
    7. >>> not in L1  
    8. False  
    9. >>> not in L1  
    10. True  
    11. >>> len(L1)  
    12. 5  
    13. >>> max(L1)  
    14. 4  
    15. >>> min(L1)  
    16. 1  


    操作:

    [cpp] view plain copy
     
     print?
    1. >>> #赋值  
    2. >>> L1[1] = 5  
    3. >>> L1  
    4. [1, 5, 3, 4, 2]  
    5. >>> #删除  
    6. >>> del L1[1]  
    7. >>> L1  
    8. [1, 3, 4, 2]  
    9. >>> #分片赋值  
    10. >>> L1[2:] = [6,7,8]  
    11. >>> L1  
    12. [1, 3, 6, 7, 8]  
    13. >>> L1[1:3] = []  
    14. >>> L1  
    15. [1, 7, 8]  

    list 的函数:

    append( x ) 是将 x 作为一个元素添加到列表的末尾,即使 x 是一个列表

    [python] view plain copy
     
     print?
    1. >>> L1  
    2. [1, 2, 7, 8]  
    3. >>> L1.append(3)  
    4. >>> L1  
    5. [1, 2, 7, 8, 3]  
    6. >>> L1.append([4,5])  
    7. >>> L1  
    8. [1, 2, 7, 8, 3, [4, 5]]  
    9. >>> in L1  
    10. False  



    count( x) 统计 x 在列表中出现的次数

    [python] view plain copy
     
     print?
    1. >>> L1 = [1, 2, 7, 8]  
    2. >>> L1.count(2)  
    3. 1  
    4. >>> L1.count(3)  
    5. 0  



    extend( x ) 将x 作为一个列表与原列表合并,添加到末尾。若不是列表,则编译器尝试将 x 转换为列表然后执行操作,不成功就会报错

    [python] view plain copy
     
     print?
    1. >>> L1  
    2. [1, 2, 7, 8]  
    3. >>> L1.extend([4,5])  
    4. >>> L1  
    5. [1, 2, 7, 8, 4, 5]  
    6. >>> in L1  
    7. True  



    index ( x ) 返回 x 在列表中的坐标,若 x 不在列表中会出错

    [python] view plain copy
     
     print?
    1. >>> L1.index(2)  
    2. 1  

    insert( i , x) 在位置i 插入元素x

    [python] view plain copy
     
     print?
    1. >>> L1  
    2. [1, 2, 7, 8, 4, 5]  
    3. >>> L1.insert(0,'a')  
    4. >>> L1  
    5. ['a', 1, 2, 7, 8, 4, 5]  
    6. >>> L1.insert(-1,'b')  
    7. >>> L1  
    8. ['a', 1, 2, 7, 8, 4, 'b', 5]  

    pop( i ) 删除位置 i 的元素并将它返回,默认可以不写 i ,删除最后一个元素,不存在会出错

    [python] view plain copy
     
     print?
    1. >>> L1 = [1, 2, 7, 8]  
    2. >>> L1.pop(1)  
    3. 2  
    4. >>> L1  
    5. [1, 7, 8]  
    6. >>> L1.pop()  
    7. 8  
    8. >>> L1  
    9. [1, 7]  

    remove( x ) 移除在 列表中 x 的第一个匹配项,x 不存在会出错

    [python] view plain copy
     
     print?
    1. >>> L1.remove(2)  
    2. >>> L1  
    3. [1, 7, 8]  



    reverse() 将列表逆序

    [python] view plain copy
     
     print?
    1. >>> L1 = [1, 2, 7, 8]  
    2. >>> L1.reverse()  
    3. >>> L1  
    4. [8, 7, 2, 1]  



    sort 将原列表排序,返回None,有两个可选参数,key 和 reverse,默认为升序排列

    [python] view plain copy
     
     print?
    1. >>> L1  
    2. [8, 7, 2, 1]  
    3. >>> L1.sort()  
    4. >>> L1  
    5. [1, 2, 7, 8]  
    6. >>> L1.sort(reverse = True)  
    7. >>> L1  
    8. [8, 7, 2, 1]  
    [python] view plain copy
     
     print?
    1. >>> L1 = ['a','ccc','abcd','bc','cd','abc']  
    2. >>> L1.sort(key = len)  
    3. >>> L1  
    4. ['a', 'bc', 'cd', 'ccc', 'abc', 'abcd']  


    元组(tuple)
    元组为不可修改的列表
    一个元素的元组表示为 ( 1 , )

    [python] view plain copy
     
     print?
    1. >>> x = (1,)  
    2. >>> type(x)  
    3. <class 'tuple'>  
    4. >>> x = (1)  
    5. >>> type(x)  
    6. <class 'int'>  

    元组可转换成列表,反之亦然。
    内建的 tuple() 函数接受一个列表参数,并返回一个包含同样元素的元组,而 list() 函数接受一个元组参数并返回一个列表。
    从效果上看, tuple() 冻结列表,而 list() 融化元组。

    [python] view plain copy
     
     print?
    1. >>> x = [1,2,4,3,1]  
    2. >>> y = (1,2,4,3,1)  
    3. >>> type(x)  
    4. <class 'list'>  
    5. >>> type(y)  
    6. <class 'tuple'>  
    7. >>> z = tuple(x)  
    8. >>> z  
    9. (1, 2, 4, 3, 1)  
    10. >>> z = list(y)  
    11. >>> z  
    12. [1, 2, 4, 3, 1]  

    可以用列表 或 元组 进行一次多赋值:

    [python] view plain copy
     
     print?
    1. >>> L1 = (1,2,4)  
    2. >>> (x, y, z) = L1  
    3. >>> x  
    4. 1  
    5. >>> y  
    6. 2  
    7. >>> z  
    8. 4  
    [python] view plain copy
     
     print?
    1. >>> L1 = [1,2,4]  
    2. >>> (x,y,z) = L1  
    3. >>> x  
    4. 1  
    5. >>> y  
    6. 2  
    7. >>> z  
    8. 4  



    [] ,和 () 在布尔值中表示 False

    参考文献:http://blog.csdn.net/jcjc918/article/details/9356047

  • 相关阅读:
    ios存储 plist 偏好设置 自定义对象存储
    Spring中Bean的生命中期与InitializingBean和DisposableBean接口
    Spring中BeanPostProcessor
    cxf
    ios快捷键
    UIPickerView
    ios通知-kvo
    7.python 装饰器
    5.python内置函数
    4.python 深拷贝 浅拷贝 函数 匿名函数lambda
  • 原文地址:https://www.cnblogs.com/fhsy9373/p/7193461.html
Copyright © 2020-2023  润新知