• python-第01章05章节-列表使用


    1.列表简单的增删改查,反转,排序

    #如果想存储很多信息,可以存储和改写,需要数据类型为列表
    names = ["han","wang","liu" ,"hanjiali","han" ]
    print(names[0],names[3])
    #假如列表很多人,一起取中间两个人
    print(names[1:3])#切片,这个格式为左闭右开,顾头不顾尾
    #不知道有多长,但是取最后一个,怎么取
    print(names[-1])
    #想取出最后两个值
    print(names[-2:])#想取出最后一个,但是不顾后面的,所以默认就可以
    #人太少,想要放值(增)
       #1,直接写进去
    names = ["han","wang","liu" ,"hanjiali"]
    names.append("zhang" )
       #2,想插入放名字,不能批量插入
    names.insert(1,"wanglong")#写想插入的位置,加插入的名字
    print(names)
    #想要改名字
    names[2] = 'luojia'
    print(names)
    #删除
    #1.
    names.remove("han")
    #2.
    del names[1]
    print(names)
    #3.
    names.pop()#删除最后一个
    name.pop(1)#= del names[1]
    #想要找人,前提列表很长
    #1.将名字传给列表,找名字
    print(names[names.index("liu")])
    #2.重名的人,怎么查找
    print(name.count("han"))
    #清空表格
    print(names.clear())
    #反转列表
    names.reverse()
    #排序
    names.sort()
    #
    name = [1,2,3,4,5]
    names.extend(name)
    print(names,name)
    

     2.列表的copy(浅copy,深copy)

    #copy的用法,只cope第一层,为浅cope
    names = ["han","wang","liu","hanjiali","han" ]
    
    name2 = names.copy()
    names[2] = "yu"
    print(names)
    print(name2)
    

     运行结果:

    ['han', 'wang', 'yu', 'hanjiali', 'han']
    ['han', 'wang', 'liu', 'hanjiali', 'han']
    

    深copy

    import copy
    names = ["han","wang","liu","hanjiali","han" ]
    name2 = copy.deepcopy(names)
    print(names)
    print(name2)
    name1 = ["han","wang","liu","hanjiali","han" ]
    name1[3] = "马瑞龙"
    name3 = copy.deepcopy(name1)
    
    print(name1)
    print(name3)
    

     运行结果:

    ['han', 'wang', 'liu', 'hanjiali', 'han']
    ['han', 'wang', 'liu', 'hanjiali', 'han']
    ['han', 'wang', 'liu', '马瑞龙', 'han']
    ['han', 'wang', 'liu', '马瑞龙', 'han']
    

     3.列表跳着打印

    names = ["han","wang","liu","hanjiali","han" ]
    print(names[0:5:2])
    for i in names:
        print(i)
    

     0和-1可以都去掉print(names[::2])

    运行结果:

    ['han', 'liu', 'han']
    han
    wang
    liu
    hanjiali
    han
    
  • 相关阅读:
    Hadoop学习笔记—20.网站日志分析项目案例(二)数据清洗
    python四舍五入保留2位小数
    查看python中的keywords(关键字)和modules
    c:forEach实现动态select标签
    解决Windows上数据库密码忘记问题
    Java读取properties文件总结
    URL地址最大长度问题
    Servlet生命周期
    解决mysql 数据库连接密码
    Java中int与integer的区别
  • 原文地址:https://www.cnblogs.com/hanjiali/p/11303978.html
Copyright © 2020-2023  润新知