美文网首页
python02-列表简介

python02-列表简介

作者: 程一川 | 来源:发表于2023-02-27 16:08 被阅读0次

    1、列表的定义

    列表是由一系列按特定顺序排列的元素组成,可包含字母、数字等等,其中元素之间没有任何关系。
    在python中,用方括号[ ]来表示列表,并用逗号来分隔其中的元素,如下所示:

    bicycles = ['trek','cannodale','redline','specialized']
    print(bicycles)
    输出结果:
    ['trek', 'cannodale', 'redline', 'specialized']
    

    (1)访问列表元素

    bicycles = ['trek','cannodale','redline','specialized']
    print(bicycles[0])
    输出结果:
    trek
    

    又如:

    bicycles = ['trek','cannodale','redline','specialized']
    print(bicycles[0].title())
    输出结果:
    Trek
    

    (2)索引从0而不是1开始
    在python中,第一个元素的索引为0,而不是1,如下所示:

    bicycles = ['trek','cannodale','redline','specialized']
    print(bicycles[1])
    print(bicycles[3])
    输出结果:
    cannodale
    specialized
    

    python为访问最后一个列表元素提供了一种特殊语法,通过将索引指定为-1,可以让python返回最后一个列表元素:

    bicycles = ['trek','cannodale','redline','specialized']
    print(bicycles[-1])
    print(bicycles[-3])
    输出结果:
    specialized
    cannodale
    

    (3)使用列表中的各个值
    可像使用其他变量一样使用列表中的各个值,如:

    bicycles = ['trek','cannodale','redline','specialized']
    message = "My first bicycle was a " +" " + bicycles[0].title() + "."
    print(message)
    输出结果:
    My first bicycle was a  Trek.
    

    2、修改、添加和删除元素

    (1)修改列表元素
    修改列表元素的语法与访问列表元素的语法类似。要修改列表元素,可指定列表名和要修改的元素的索引,再指定该元素的新值。
    如:

    motorcycles = ['honda','yamaha','suzuki']
    print(motorcycles )
    输出结果:
    ['honda', 'yamaha', 'suzuki']
    
    motorcycles[0] = 'ducati'
    print(motorcycles )
    输出结果:
    ['ducati', 'yamaha', 'suzuki']
    

    (2)在列表中添加元素
    1)在列表末尾添加元素

    motorcycles = ['honda','yamaha','suzuki']
    motorcycles.append('ducati')
    print(motorcycles )
    输出结果:
    ['honda', 'yamaha', 'suzuki', 'ducati']
    

    2)在列表中插入元素
    使用方法insert()可在列表的任何位置添加新元素,为此,需要指定新元素的索引和值:

    motorcycles = ['honda','yamaha','suzuki']
    motorcycles.insert(0,'ducati')
    print(motorcycles)
    输出结果:
    ['ducati', 'honda', 'yamaha', 'suzuki']
    

    (3)从列表中删除元素
    1)使用del语句删除元素
    如果知道要删除的元素在列表中的位置,可以使用del语句,删除之后,就无法再访问删除的值:

    motorcycles = ['honda','yamaha','suzuki']
    del motorcycles[0]
    print(motorcycles)
    输出结果:
    ['yamaha', 'suzuki']
    

    2)使用方法pop()删除元素
    有时候,需要将元素从列表中删除,并接着使用他的值。方法pop()可以删除列表末尾的元素,并让你能够接着使用它。术语弹出(pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。

    motorcycles = ['honda','yamaha','suzuki']
    popped_motorcycle = motorcycles.pop()
    print(motorcycles )
    print(popped_motorcycle)
    输出结果:
    ['honda', 'yamaha']
    suzuki
    

    3)弹出列表中任何位置处的元素
    实际上,你可以使用pop()来删除列表中任何位置的元素,只需要再括号中指定要删除的元素的索引即可:

    motorcycles = ['honda','yamaha','suzuki']
    second_owned = motorcycles.pop(1)
    print('The second motorcycle I owned was a ' + second_owned.title() + '.')
    print(motorcycles )
    输出结果:
    The second motorcycle I owned was a Yamaha.
    ['honda', 'suzuki']
    

    综上所诉,如果不确定使用del语句还是pop()方法,下面是一个简单的判断标准:如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del语句;如果你要在删除元素后还能继续使用它,就是用pop()。

    4)根据值删除元素
    有时候,并不知道要从列表中删除的值所在的位置,如果只知道要删除的元素的值,可以使用方法remove()

    motorcycles = ['honda','yamaha','suzuki','ducati']
    motorcycles.remove('ducati')
    print(motorcycles)
    输出结果:
    ['honda', 'yamaha', 'suzuki']
    

    使用remove()从列表中删除元素时,也可以接着使用它的值:

    motorcycles = ['honda','yamaha','suzuki','ducati']
    a = 'ducati'
    motorcycles.remove(a)
    print(motorcycles)
    print("\nA " + a.title() + " is too expensive for me.")
    输出结果:
    ['honda', 'yamaha', 'suzuki']
    
    A Ducati is too expensive for me.
    

    注:方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。

    3、组织列表

    (1)使用方法sort()对列表进行永久性排序

    cars = ['bmw','audi','toyota','subaru']
    cars.sort()
    print(cars)
    输出结果:
    ['audi', 'bmw', 'subaru', 'toyota']
    

    上述sort()方法,使列表元素按字母顺序排列,再也无法回复到原来的排列顺序。
    还可以按与字母顺序相反的顺序排列列表元素,为此,只需向sort()方法传递参数reverse=True:

    cars = ['bmw','audi','toyota','subaru']
    cars.sort(reverse=True)
    print(cars)
    输出结果:
    ['toyota', 'subaru', 'bmw', 'audi']
    

    同样,这种方法对列表元素排列顺序的修改也是永久性的。

    (2)使用函数sorted()对列表进行临时排序
    要保留列表元素原来的排列顺序,同时以特定的顺序呈现他们,可以使用函数sorted():

    图片.png

    如果要按照字母顺序相反的顺序显示列表,也可以向函数sorted()传递参数reverse=True。

    (3)倒着打印列表
    要反转列表元素的排列顺序,可以使用方法reverse(),例如:

    图片.png
    方法reverse()永久性地修改列表元素的排列顺序,但可以随时恢复到原来的排列顺序,为此,只需对列表再次调用reverse()即可。

    (4)确定列表的长度
    使用函数len()可快速获悉列表长度,例如:

    图片.png

    相关文章

      网友评论

          本文标题:python02-列表简介

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