• 列表的常用的方法(内建函数)


    方法是一个与某些对象有紧密联系的函数,对象可能是列表、数字,也可能是字符串或者其他类型的对象。

    一般来说方法可以这样调用:对象.方法(参数)

    列表常见的一些方法(方法也是函数的一种类型)。

    序号 函数                               功能
    1 append()  
    2 extend()  
    3 count()  
    4 index()  
    5 insert()  
    6 pop()  
    7 reverse()  
    8 sort()  
    9 remove()  

     增:append追加单个元素在末尾

       extend可迭代增加单个或多个元素。增加的可能是元祖、列表或者字符串

       insert按照索引来添加单个元素

    删:pop按照索引去删并且返回所删除的值。

           remove按照元素去删。

      clear清空列表

      del删除元素或者列表

    改:要么用索引,要么用切片

    1.append:在列表末尾追加新的对象。

    >>> number = [1,2,3,4,5]
    >>> number.append(6)
    >>> number
    [1, 2, 3, 4, 5, 6]

    2.count:统计某个元素在列表中出现的次数。

    >>> number = [1,2,3,1,1,2,4]
    >>> number.count(1)
    3
    >>> number.count(4)
    1

    3.extend:可以在列表的末尾一次追加另一张表。

    >>> num1 = [1,2,3]
    >>> num2 = [5,6,7]
    >>> num1.extend(num2)
    >>> num1
    [1, 2, 3, 5, 6, 7]

    看起来有点像num1 + num2,但是二者的机制完全不一样。num1 + num2生成了一张新的表,开辟了新的字符空间。而num1.extend(num2)只是改变了num1,可以通过查看内存地址来验证。

    >>> num1 = [1,2,3]
    >>> num2 = [5,6,7]
    >>> id(num1)
    2027796247112  
    >>> id(num2)
    2027796246600
    >>> num1.extend(num2)
    >>> num1
    [1, 2, 3, 5, 6, 7]
    >>> id(num1)
    2027796247112    #num1的内存地址没变

    在列表上追加一个元祖也是可以的,但是反过来,在一个元祖上追加一个列表不行,因为extend是针对列表的方法。

    >>> list1 = [1,2.3]
    >>> tuple = (4,5,6)
    >>> list1.extend(tuple)
    >>> list1
    [1, 2.3, 4, 5, 6]
    
    >>> tuple.extend(list1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'tuple' object has no attribute 'extend' #元祖对象没有‘extend’属性。
    >>> ssg = [1,2,3,4]
    >>> ssg.extend('5678')
    >>> ssg
    [1, 2, 3, 4, '5', '6', '7', '8']
    >>> ssg = [1,2,3,4]
    >>> ssg.append('5678')
    >>> ssg
    [1, 2, 3, 4, '5678']

    4.index:从序列中查找某个值第一个匹配项的索引位置。

    [1, 2, 3, 5, 6, 7, 5, 6, 7]
    >>> num.index(7)
    5

    5.insert:将对象插入到列表中

    >>> name = ['kebi','maoxian','xiaoniao']
    >>> name.insert(1,'xinye')
    >>> name
    ['kebi', 'xinye', 'maoxian', 'xiaoniao']

    6.pop:从列表中按照索引移除一个元素(默认是最后一个),同时返回该元素的值。

    >>> name
    ['kebi', 'xinye', 'maoxian', 'xiaoniao']
    >>> name.pop()
    'xiaoniao'
    >>> name
    ['kebi', 'xinye', 'maoxian']   #默认移除最后一个
    >>> name.pop(0)       #依据索引来进行操作
    'kebi'                            #同时返回移除的值
    >>> name
    ['xinye', 'maoxian']

    7.remove:移除列表中某个元素的第一个匹配项。

    >>> name = ['kebi','maoxian','xinye','xiaoniao','xinye','maoxian']
    >>> name.remove('maoxian')
    >>> name
    ['kebi', 'xinye', 'xiaoniao', 'xinye', 'maoxian']
    >>> name.remove('xinye')     #再来看一遍
    >>> name
    ['kebi', 'xiaoniao', 'xinye', 'maoxian']

    8.reverse:将列表中的元素反向存放。

           反转不是排序,

    >>> number = [4,3,2,1]
    >>> number.reverse()
    >>> number
    [1, 2, 3, 4]

    9.sort:再原位置对列表进行排序

    >>> number = [4,2,1,5,9]
    >>> number.sort()   #没有返回值意味着改变了原表
    >>> number
    [1, 2, 4, 5, 9]

    对于字符串来说也是可行的。

    ['kebi', 'xiaoniao', 'xinye', 'maoxian']
    >>> name.sort()
    >>> name
    ['kebi', 'maoxian', 'xiaoniao', 'xinye']
  • 相关阅读:
    20165231 2017-2018-2《Java程序设计》课程总结
    2017-2018-2 20165231 实验五 网络编程与安全
    2017-2018-2 20165231 实验四 Android程序设计
    2017-2018-2 20165231 实验三 敏捷开发与XP实践
    20165231 2017-2018-2 《Java程序设计》第9周学习总结
    20165231 结对编程四则运算第二阶段总结
    20165231 2017-2018-2 《Java程序设计》第8周学习总结
    20165115 第二周学习总结
    20165115 第一周学习总结
    20165115 C语言学习心得
  • 原文地址:https://www.cnblogs.com/yangmingxianshen/p/7702842.html
Copyright © 2020-2023  润新知