zoukankan      html  css  js  c++  java
  • 列表的魔法

    #将字符串转换成列表
    test = 'lemonluoluo'
    print(list(test))
    ——————————执行结果——————————
    ['l', 'e', 'm', 'o', 'n', 'l', 'u', 'o', 'l', 'u', 'o']
    
    
    
    #追加值到列表
    li = [1,2,3,4,5]
    print(li)
    li.append([123,'你好啊'])
    print(li)
    ——————————执行结果——————————
    [1, 2, 3, 4, 5]
    [1, 2, 3, 4, 5, [123, '你好啊']]
    
    
    
    #清空列表
    li = [1,2,3,4,5,6,1]
    li.clear()
    print(li)
    ——————————执行结果——————————
    []
    
    
    
    #复制(浅拷贝)
    li = [1,2,3,4]
    aa = li.copy()
    print(aa)
    ——————————执行结果——————————
    [1, 2, 3, 4]
    
    
    
    #计算元素的个数
    test = [1,2,3,1,1,1]
    print(test.count(1))
    ——————————执行结果——————————
    4
    
    
    
    #可迭代对象追加到列表
    test = [1]
    test.extend(['哈哈'])
    test.extend('哈哈')
    print(test)
    ——————————执行结果——————————
    [1, '哈哈', '哈', '哈']
    
    
    #在指定位置插入自定义元素
    test = [11,22,33,44]
    test.insert(2,10)
    print(test)
    ——————————执行结果——————————
    [11, 22, 10, 33, 44]
    
    
    
    
    #将值抓出来,源列表失去该值,无指定默认是最后一个值
    list = [11,22,33,44]
    print(list.pop(0))
    print(list)
    ——————————执行结果——————————
    11
    [22, 33, 44]
    
    
    
    #删除列表中的指定值
    test = [11,22,33,44]
    test.remove(22)
    print(test)
    ——————————执行结果——————————
    [11, 33, 44]
     
    
    #反转列表
    test = [1,2,3,4,5,6]
    test.reverse()
    print(test)
    ——————————执行结果——————————
    [6, 5, 4, 3, 2, 1]
    
    
    
    #列表的排序,默认从小到大,参数reverse=True,可以从大到小
    test = [5,2,28,9,3,124,68,2,0]
    test.sort()
    print(test)
    test.sort(reverse=True)
    print(test)
    ——————————执行结果——————————
    [0, 2, 2, 3, 5, 9, 28, 68, 124]
    [124, 68, 28, 9, 5, 3, 2, 2, 0]
     
     
    

      

  • 相关阅读:
    金色酒类企业dedecms模板
    hdu 1533 最小费用最大流
    计算几何算法
    博弈总结
    图搜索总结
    一般图匹配题集 转自夏天的风
    极大极小过程题集 转自夏天的风
    dancing links 题集转自夏天的风
    网络流题集转自夏天的风
    nyoj 547 优先队列
  • 原文地址:https://www.cnblogs.com/lemonbk/p/10612837.html
Copyright © 2011-2022 走看看