zoukankan      html  css  js  c++  java
  • 列表内部常用功能和使用方法-课堂笔记及课后总结

    查看列表内部方法,使用help(list)、dir(list)来查看。

    1、append:向列表中添加元素,添加到尾部

    L.append(object) -- append object to end

    1 li = ['parr','123','456']
    2 li.append('abc')
    3 print(li)
    4 
    5 #屏幕打印:['parr', '123', '456', 'abc']
    View Code

    2、count:返回值在列表中出现的次数

    L.count(value) -> integer -- return number of occurrences of value

    li = ['parr','123','456','123']
    result = li.count('123')
    print(result)
    
    #屏幕打印:2
    View Code

    3、extend:扩展对象到列表,对象可以是元组,字符串,列表等

    L.extend(iterable) -- extend list by appending elements from the iterable

     1 li = ['parr','123']
     2 li1 = ['789']
     3 tuple1 = ('456')
     4 dic1 = {'k1':'v1'}
     5 li.extend(li1)
     6 print(li)
     7 
     8 #屏幕打印:['parr', '123', '789']
     9 
    10 li = ['parr','123']
    11 li1 = ['789']
    12 tuple1 = ('哈哈')
    13 dic1 = {'k1':'v1'}
    14 li.extend(tuple1)
    15 print(li)
    16 
    17 #屏幕打印:['parr', '123', '哈', '哈']
    18 
    19 li = ['parr','123']
    20 li1 = ['789']
    21 tuple1 = ('哈哈')
    22 dic1 = {'k1':'v1'}
    23 li.extend(dic1)
    24 print(li)
    25 
    26 #屏幕打印:['parr', '123', 'k1']
    View Code

    4、index:返回值出现的第一个位置的索引值

    L.index(value, [start, [stop]]) -> integer -- return first index of value.

    li = ['parr','123','parr']
    result = li.index('parr')
    print(result)
    
    #屏幕打印:0
    View Code

    5、insert:插入对象从指定的索引位置开始

    L.insert(index, object) -- insert object before index

    li = ['parr','123','parr']
    result = li.insert(2,'456')
    print(li)
    
    #屏幕打印:['parr', '123', '456', 'parr']
    View Code

    6、pop:删除指定索引的值并返回被删除的值,如果索引未指定默认为最后,索引超出或列表为空会出现索引错误

    L.pop([index]) -> item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.

    li = ['parr','123','parr']
    result = li.pop()
    print(li)
    print(result)
    
    #屏幕打印:['parr', '123']
                      parr    
    View Code

    7、remove:删除指定的值第一次出现的位置,也就是说只会删除一次指定的值,如果值不存在会出现ValueError

    L.remove(value) -- remove first occurrence of value.
    Raises ValueError if the value is not present.

    li = ['parr','123','parr']
    result = li.pop(1)
    print(li)
    print(result)
    
    #屏幕打印:['parr', 'parr']
    #               123 
    View Code
     1 li = ['parr','123','parr']
     2 result = li.pop(5)
     3 print(li)
     4 print(result)
     5 
     6 #Index超出范围报错:
     7 #Traceback (most recent call last):
     8 #  File "D:/study/int_class.py", line 24, in <module>
     9 #    result = li.pop(5)
    10 #IndexError: pop index out of range
    View Code

    8、reverse:翻转列表

    L.reverse() -- reverse *IN PLACE* 

    li = ['parr','456','123']
    li.reverse()
    print(li)
    
    #屏幕打印:['123', '456', 'parr']
    View Code

    9、sort:列表排序,字符串和列表不能一起排序,会报错

    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;

    li = [1,2,5,6,9,1,56,12]
    li.sort()
    print(li)
    
    #屏幕打印:[1, 1, 2, 5, 6, 9, 12, 56]
    View Code
    注:以上纯属个人课堂笔记及总结,如有错的地方,请大家帮忙纠正,谢谢!
  • 相关阅读:
    《构建之法》阅读笔记07
    学习进度条——第六周
    《构建之法》阅读笔记06
    团队开发
    《构建之法》阅读笔记05
    数组3——返回二维数组中最大联通子数组的和
    学习进度条——第五周
    坯布检验管控系统
    DateTime日期格式转换,不受系统格式的影响
    多层下firebird自增长字段的处理
  • 原文地址:https://www.cnblogs.com/parr2017/p/7792959.html
Copyright © 2011-2022 走看看