Python基础: 列表
发布时间:2024-12-05 05:59 浏览量:5
列表是按特定顺序排列的项的集合,可以包含任何类型的数据。
语法:列表使用方括号 创建,元素用逗号分隔。
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles)#Output ['trek', 'cannondale', 'redline', 'specialized']访问元素:使用索引访问元素,从 0 开始。负索引可用于访问列表末尾的元素。
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0])#Output trekbicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles[0].title)#OutPutTrek可以使用 append、insert、del、pop 和 remove 等各种方法更改、添加或删除元素。
向列表添加元素
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles)#Outuput ['honda', 'yamaha', 'suzuki'] motorcycles.append('Ducati') print(motorcycles)#Output ['honda', 'yamaha', 'suzuki', 'ducati']将元素插入 List
motorcycles = ['honda', 'yamaha', 'suzuki'] motorcycles.insert(0, 'ducati') print(motorcycles)#Output ['ducati', 'honda', 'yamaha', 'suzuki']从 List 中删除元素
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) del motorcycles[0] print(motorcycles)#Output ['honda', 'yamaha', 'suzuki'] ['yamaha', 'suzuki']使用 pop 方法删除项
motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles) popped_motorcycle = motorcycles.pop print(motorcycles) print(popped_motorcycle)#Output ['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha'] suzuki motorcycles = ['honda', 'yamaha', 'suzuki'] last_owned = motorcycles.pop print(f"The last motorcycle I owned was a {last_owned.title}.")#Output The last motorcycle I owned was a Suzuki.从列表中的任意位置弹出项目
motorcycles = ['honda', 'yamaha', 'suzuki'] first_owned = motorcycles.pop(0) print(f"The first motorcycle I owned was a {first_owned.title}.")#Output The first motorcycle I owned was a Honda.按值删除项
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles) motorcycles.remove('ducati') print(motorcycles)#Output ['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki']motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] print(motorcycles)too_expensive = 'ducati'motorcycles.remove(too_expensive) print(motorcycles)print(f"\nA {too_expensive.title} is too expensive for me.")#Output ['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki'] A Ducati is too expensive for me.使用 sort 方法对列表进行永久排序:
使用 sort 按字母顺序对列表进行排序。cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort print(cars)>> ['audi', 'bmw', 'subaru', 'toyota']还可以使用 sort(reverse=True) 反向排序。cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort(reverse=True) print(cars)>> ['toyota', 'subaru', 'bmw', 'audi']临时对列表进行排序:
使用 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']反转列表:
使用 reverse 反转列表的顺序。 cars = ['bmw', 'audi', 'toyota', 'subaru'] print(cars) cars.reverse print(cars)>> ['bmw', 'audi', 'toyota', 'subaru'] ['subaru', 'toyota', 'audi', 'bmw']求列表的长度:
使用 len 查找列表中的项数。>>> cars = ['bmw', 'audi', 'toyota', 'subaru'] >>> len(cars)>> 4排序注意事项:在处理大小写混合值时,排序会变得复杂,并且可能需要其他注意事项才能正确排序。
常见索引错误:
当尝试访问列表中不存在的索引时,会发生这种情况。motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[3])# This example results in an index error:Traceback (most recent call last): File "motorcycles.py", line 2, in print(motorcycles[3]) ~~~~~~~~~~~^^^ IndexError: list index out of rangePython 列表是零索引的;第一项位于索引 0 处。
第三项是通过摩托车[2]访问的,而不是用摩托车[3] 来访问的。
访问最后一项:
使用 -1 可访问列表的最后一项。motorcycles = ['honda', 'yamaha', 'suzuki'] print(motorcycles[-1])#The index -1 always returns the last item in a list, in this case the value 'suzuki'>>suzuki访问空列表中的最后一项(例如 motorcycles[-1])也会导致 IndexError。motorcycles = print(motorcycles[-1])# No items are in motorcycles, so Python returns another index error:Traceback (most recent call last): File "motorcyles.py", line 3, in print(motorcycles[-1]) ~~~~~~~~~~~^^^^ IndexError: list index out of range故障排除:如果您遇到 IndexError,请使用 len 检查列表的长度或打印列表以了解其内容。