zoukankan      html  css  js  c++  java
  • 总结:数列的一些方法

        数列的方法有那么几种:list.append(obj) ---------添加元素到数列的末尾  

                 list.insert(index,obj)---------按照index下标插入元素

                 list.pop(index)------------对应index的下标元素被删除,默认是删除最后一个元素

                 list.count(obj)------------统计obj元素的个数

                 list.remove(obj)----------删除重复且排在前面的元素

                 list.severse()----------反向数列中的元素

                 list.sort()----------数列元素排序

                 list.index(obj)--------查看该元素的下标是多少

                 list2.exten(list1)-------在list2数列结尾中添加list1数列中的元素

                                      zip(list1,list2)------合并列表

    例子:

    1)list.append(obj)

    list1 = [1,2,3,4,'中国','美国']
    
    list1.append(5)
    
    print(list1)  
    
    # 运行时输出结果
    
     [1,2,3,4,'中国','美国',5]
    

    2) list.insert(2,'加拿大')

    list2 =  [1,2,3,4.0,'中国','美国']
    
    list2.insert(2,'加拿大')
    
    print(list2)

    # 运行时输出结果

     [1,2,'加拿大',3,4,'中国','美国']

    3)list.pop()

    list3 = [1,2,'加拿大',3,4,'中国','美国']
    
    print(list3.pop())
    
    #输出结果
    
    [1,2,'加拿大',3,4,'中国']

    print(list3.pop(-1))
    #输出结果
    [1,2,'加拿大',3,4,'中国']

    print(list3.pop(3))

    #输出结果
    [1,2,3,4,'中国','美国']

    4)list.count(jbo)

    list4 = [1,2,3,3,4,'中国',3]
    
    list4.count(3)
    
    #输出结果
    
    3
    

    5)list.remove(obj)

    list5 = [1,2,'加拿大',3,4,'中国','美国']
    list5.remove('美国')
    print(list5) # 输出结果 [1,2,'加拿大',3,4,'中国']

    6)list.severse()

    list7 = [1,3,4,2,'a','r','h']
    
    list6.reverse()
    
    print(list6)
    
    #输出结果
    ['h', 'r', 'a', 2, 4, 3, 1]

    7)list.sort()

    list7 = [2,4,6,3,5,1]
    list7.sort()
    print(list7)
    
    #输出结果
    
    [1, 2, 3, 4, 5, 6]
    

    8)list.index(obj)

    list8 =  ['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6]
    
    list8.index('b')
    
    # 输出结果
    
    1
    

      

    9)list2.exten(list1)

    a = [1, 2, 3, 4, 5, 6]
    b = ['a','b','c','d']
    
    b.extend(a)
    
    print(b)
    
    # 输出结果
    
    ['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 6]
    

    10)zip(lis1,list2)

     L1 = [1,3,5,7]
     L2 = [2,4,6,8]
    
    for (i,j) in (L1,L2)
         print(i,j)
    
    # 遍历结果
    1 2
    3 4
    5 6
    7 8
    

      

  • 相关阅读:
    工作
    失败
    理想和一些未来的计划
    安静
    重新开始
    如何度过周末
    放假
    WPF学习笔记-数据采集与监控项目01-登录界面
    VS2017-断点感叹号问题,调试代码显示“当前无法命中断点,还没有为该文档加载任何符号”
    WPF-MVVM模式-表现层的UI框架【学习笔记】
  • 原文地址:https://www.cnblogs.com/xiangzhe/p/4374142.html
Copyright © 2011-2022 走看看