• Python-列表简介


    Python中,用方括号[]表示列表,用逗号来分割其中的元素。列表由一系列按照特定顺序排列的元素组成。可以包含字母,数字等。

    bicycles = ['trek', 123, 'cannodale', 'redline', 'specialized']
    print(bicycles)
    #['trek', 123, 'cannodale', 'redline', 'specialized']
    
    • 1
    • 2
    • 3

    访问列表元素。列表索引从0开始,而不是1. -1可以直接访问列表最后一个元素。

    bicycles = ['trek', 123, 'cannodale', 'redline', 'specialized']
    print(bicycles[0])
    print(bicycles[-1])
    #trek
    #specialized
    
    • 1
    • 2
    • 3
    • 4
    • 5

    修改元素

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    motorcycles[0] ='ducati'
    print(motorcycles)
    #['honda', 'yamaha', 'suzuki']
    #['ducati', 'yamaha', 'suzuki']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用append()方法 在列表尾部添加元素

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    motorcycles.append('ducati')
    print(motorcycles)
    #['honda', 'yamaha', 'suzuki']
    #['honda', 'yamaha', 'suzuki', 'ducati']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用insert()方法在列表中插入元素

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    motorcycles.insert(0,'ducati')
    print(motorcycles)
    #['honda', 'yamaha', 'suzuki']
    #['ducati', 'honda', 'yamaha', 'suzuki']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用del语句删除元素

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    del motorcycles[0]
    print(motorcycles)
    #['honda', 'yamaha', 'suzuki']
    #['yamaha', 'suzuki']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用pop()方法删除列表中的元素,默认的是删除尾部元素,可以在括号内设置要删除元素的索引
    del语句和pop方法的区别。 如果要删掉列表中的一个元素,并且不在以任何方式使用它,就用del语句;如果要删除元素后继续使用它,就用方法pop()。

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    popped_motorcycle = motorcycles.pop()
    print(motorcycles)
    print(popped_motorcycle)
    #['honda', 'yamaha', 'suzuki']
    #['honda', 'yamaha']
    #suzuki
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    根据值删除元素,可以使用方法remove()

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    motorcycles.remove('yamaha')
    print(motorcycles)
    #['honda', 'yamaha', 'suzuki']
    #['honda', 'suzuki']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用方法sort()对列表进行永久性排序,设置参数 reverse=True 可以将列表按照与字母顺序相反的顺序排序

    cars = ['bmw', 'audi','toyota','subaru']
    cars.sort()
    print(cars)
    #['audi', 'bmw', 'subaru', 'toyota']
    cars.sort(reverse=True)
    print(cars)
    #['toyota', 'subaru', 'bmw', 'audi']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用函数sorted()对列表进行临时排序

    cars = ['bmw', 'audi','toyota','subaru']
    print("Here is the original list: ")
    print(cars)
    print("\nHere is the sorted list: ")
    print(sorted(cars))
    print("\nHere is the original list again: ")
    print(cars)
    #Here is the original list: 
    #['bmw', 'audi', 'toyota', 'subaru']
    #
    #Here is the sorted list: 
    #['audi', 'bmw', 'subaru', 'toyota']
    #
    #Here is the original list again: 
    #['bmw', 'audi', 'toyota', 'subaru']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    倒着打印列表。方法reverse()只是反转列表元素的排列顺序

    cars = ['bmw', 'audi','toyota','subaru']
    print(cars)
    cars.reverse()
    print(cars)
    #['bmw', 'audi', 'toyota', 'subaru']
    #['subaru', 'toyota', 'audi', 'bmw']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用函数len() 确定列表长度

    cars = ['bmw', 'audi','toyota','subaru']
    print(len(cars))
    #4
    
    • 1
    • 2
    • 3
  • 相关阅读:
    CSS进阶
    2022杭电多校联赛第八场 题解
    int* p[10]和(int*) p[10]的区别
    Echarts X轴前面加文字
    求最长的只包含两种类型的连续子数组的最大长度
    LeetCode(20)最长公共前缀【数组/字符串】【简单】
    Linux之软件包管理
    Go实现MapReduce
    19 正交试验设计
    java中流的分类
  • 原文地址:https://blog.csdn.net/xiaoxinxin2017/article/details/126266836