美文网首页python
python(2):列表

python(2):列表

作者: Z_bioinfo | 来源:发表于2022-03-26 19:33 被阅读0次

    列表由系系列按特定顺序排列的元素组成。可以包含字母表中所有字母,数字或所有家庭成员姓名,也可以添加任何东西,其中的元素之间可以没有任何关系
    通常用[]表示列表,用逗号分隔其中的元素

    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    print(bicycles)
    ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    

    1.访问列表元素,只需将元素的位置或索引告诉python即可,索引从0而不是1开始

    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    print(bicycles[0])
    print(bicycles[1])
    print(bicycles[2])
    print(bicycles[3])
    print(bicycles[4])
    trck
    cannodale
    redline
    redline
    specialized
    

    2.使用列表中的各个值

    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    message = "my first bicycle was a " + bicycles[0].title() + "."
    print(message)
    my first bicycle was a Trck.
    

    3.修改,添加和删除元素

    修改
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles[0] = 'ducati'
    print(bicycles)
    ['ducati', 'cannodale', 'redline', 'redline', 'specialized']
    
    添加,列表末尾添加,append()
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles.append('honda')
    print(bicycles)
    ['ducati', 'cannodale', 'redline', 'redline', 'specialized', 'honda']
    #也可以创建一个空列表,再使用一系列append()语句添加元素
    bicycles = []
    bicycles.append('ducati')
    bicycles.append('cannodale')
    bicycles.append('redline')
    print(bicycles)
    ['ducati', 'cannodale', 'redline']
    
    在列表中插入元素:insert(),可添加到任何位置
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles.insert(0, 'ducati')
    print(bicycles)
    ['ducati', 'trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles.insert(2, 'ducati')
    print(bicycles)
    ['trck', 'cannodale', 'ducati', 'redline', 'redline', 'specialized']
    
    删除,del,pop()
    #del删除
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    del bicycles[2]
    print(bicycles)
    ['trck', 'cannodale', 'redline', 'specialized']
    #删除,pop(),与del区分方法,如果要从列表中删除一个元素并且不在使用它,用del,如果在删除后还能继续使用,用pop
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    popped_bicycles = bicycles.pop(2)
    print(bicycles)
    print(popped_bicycles)
    ['trck', 'cannodale', 'redline', 'specialized']
    redline
    #根据值删除元素,remove()
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles.remove('redline')
    print(bicycles)
    ['trck', 'cannodale', 'redline', 'specialized']
    

    4.组织列表

    使用sort()对列表进行永久性排序,这里是按字母进行排序
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles.sort()
    print(bicycles)
    ['cannodale', 'redline', 'redline', 'specialized', 'trck']
    #按与字母顺序相反的顺序排列列表元素
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles.sort(reverse=True)
    print(bicycles)
    ['trck', 'specialized', 'redline', 'redline', 'cannodale']
    
    使用sorted()对列表进行临时排序,这里是按字母进行排序
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    
    print(sorted(bicycles))
    #加reverse=True逆序
    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    
    print(sorted(bicycles, reverse=True))
    ['trck', 'specialized', 'redline', 'redline', 'cannodale']
    

    5.倒着打印列表:反转列表元素的排列顺序,reverse()函数,永久性修改列表元素的排列顺序

    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    bicycles.reverse()
    print(bicycles)
    ['specialized', 'redline', 'redline', 'cannodale', 'trck']
    

    6.确定列表的长度:len()

    bicycles = ['trck', 'cannodale', 'redline', 'redline', 'specialized']
    len(bicycles)
    5
    

    相关文章

      网友评论

        本文标题:python(2):列表

        本文链接:https://www.haomeiwen.com/subject/fmjsjrtx.html