Python编程:从入门到实践-列表
-
访问列表中的元素
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> print(bicycles[0])
trek
>>> print(bicycles[-1])
specialized
>>> print(bicycles[0].title())
Trek
-
修改、添加和删除元素
修改列表元素
>>> motorcycles = ['honda','yamaha','suzuki']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> motorcycles[0] = 'ducati'
>>> print(motorcycles)
['ducati', 'yamaha', 'suzuki']
在列表中添加元素
>>> motorcycles = []
>>> motorcycles.append('honda')
>>> motorcycles.append('yamaha')
>>> motorcycles.append('suzuki')
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
在列表中插入元素
>>> motorcycles = ['honda','yamaha','suzuki']
>>> motorcycles.insert(0,'ducati')
>>> print(motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']
从列表中删除元素
>>> motorcycles = ['honda','yamaha','suzuki']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> del motorcycles[0]
>>> print(motorcycles)
['yamaha', 'suzuki']
使用方法pop()删除元素
方法pop()删除列表末尾的元素,并可以使用它
>>> motorcycles = ['honda','yamaha','suzuki']
>>> popped_motorcycle = motorcycles.pop()
>>> print(motorcycles)
['honda', 'yamaha']
>>> print(popped_motorcycle)
suzuki
应用:
>>> motorcycles = ['honda','yamaha','suzuki']
>>> last_owned = motorcycles.pop()
>>> print("The last motorcycles I owned was a " + last_owned.title() + ".")
The last motorcycles I owned was a Suzuki.
弹出列表中任何位置处的元素:
>>> motorcycles = ['honda','yamaha','suzuki']
>>> first_owned = motorcycles.pop(0)
>>> print('The first motorcycle I owned was a ' + first_owned.title() + '.')
The first motorcycle I owned was a Honda.
>>> print(motorcycles)
['yamaha', 'suzuki']
每当使用pop(),被弹出的元素就不再在列表中了。
使用值删除元素
>>> motorcycles = ['honda','yamaha','suzuki','ducati']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
>>> motorcycles.remove('ducati')
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
使用remove()从列表中删除的元素,也可以接着使用它的值。
>>> motorcycles = ['honda','yamaha','suzuki','ducati']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
>>> too_expensive = 'ducati'
>>> motorcycles.remove(too_expensive)
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> print("\nA " + too_expensive.title() + " is too expensive for me.")
A Ducati is too expensive for me.
方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。
-
组织列表
-
使用方法sort()对列表进行永久性排序
>>> cars = ['bmw','audi','toyoda','subaru']
>>> print(cars)
['bmw', 'audi', 'toyoda', 'subaru']
>>> cars.sort()
>>> print(cars)
['audi', 'bmw', 'subaru', 'toyoda']
方法sort()永久性地修改了列表元素的排列顺序,cars按字母顺序排列
倒序排序方式:
>>> cars = ['bmw','audi','toyoda','subaru']
>>> cars.sort(reverse=True)
>>> print(cars)
['toyoda', 'subaru', 'bmw', 'audi']
-
使用函数sorted()对列表进行临时排序
>>> cars = ['bmw','audi','toyoda','subaru']
>>> print("Here is the original list:",cars)
Here is the original list: ['bmw', 'audi', 'toyoda', 'subaru']
>>> print("Here is the original list:\n",sorted(cars))
Here is the original list:
['audi', 'bmw', 'subaru', 'toyoda']
>>> print("\nHere is the original list:",cars)
Here is the original list: ['bmw', 'audi', 'toyoda', 'subaru']
-
倒着打印列表
>>> cars = ['bmw','audi','toyoda','subaru']
>>> print(cars)
['bmw', 'audi', 'toyoda', 'subaru']
>>> cars.reverse()
>>> print(cars)
['subaru', 'toyoda', 'audi', 'bmw']
方法reverse()永久性地修改列表元素的排列顺序
-
确定列表长度
>>> cars = ['bmw','audi','toyoda','subaru']
>>> len(cars)
-
操作列表
-
遍历整个列表
>>> magicians = ['alice','david','carolina']
>>> for magician in magicians:
... print(magician)
...
alice
david
carolina
>>> for magician in magicians:
... print(magician.title() + ",that was a great trick!")
...
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!
-
在for循环之后执行一些操作
在for循环后面,没有缩进的代码只执行一次,而不会重复执行,Python根据缩进代码行与前一个代码的关系
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
magicians = ['alice','elon','carolian']
for magician in magicians:
print(magician.title() + ",that was a great trick!")
print("I can't wait to see your next trick," + magician.title() + ".\n")
print("Thank you,everyone.That was a great magic show!")
执行结果:
Alice,that was a great trick!
I can't wait to see your next trick,Alice.
Elon,that was a great trick!
I can't wait to see your next trick,Elon.
Carolian,that was a great trick!
I can't wait to see your next trick,Carolian.
Thank you,everyone.That was a great magic show!
-
创建数值列表
>>> for value in range(1,5):
... print(value)
...
1
2
3
4
使用range()创建列表
将range()作为list()的参数,输出将为一个数字列表
>>> numbers = list(range(1,5))
>>> print(numbers)
[1, 2, 3, 4]
使用函数range()时,还可以指定步长,函数range()从2开始数,然后不断加2,直到达到或超过终值11
>>> even_numbers = list(range(2,11,2))
>>> print(even_numbers)
[2, 4, 6, 8, 10]
使用函数range()几乎能够创建任何需求的数字集,例如:创建一个列表,其中包含前10个整数的平方。
>>> squares = []
>>> for value in range(1,11):
... square = value ** 2
... squares.append(square)
... print(squares)
...
[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
为了代码简洁,可不使用临时变量square
>>> squares = []
>>> for value in range(1,11):
... squares.append(value**2)
... print(squares)
...
[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
对数字列表执行简单的统计计算
>>> digits = [1,2,3,4,5,6,7,8,9,0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45
-
使用列表的一部分-切片
1、创建切片
1 citys = ['beijing', 'tianjin', 'shanghai', 'shenzhen', 'guangzhou', 'jizhou'] 2 print(citys[0:3]) 3 print(citys[:4]) 4 print(citys[2:]) 5 print(citys[-3:])
1 2 3 4 | [ 'beijing' , 'tianjin' , 'shanghai' ] [ 'beijing' , 'tianjin' , 'shanghai' , 'shenzhen' ] [ 'shanghai' , 'shenzhen' , 'guangzhou' , 'jizhou' ] [ 'shenzhen' , 'guangzhou' , 'jizhou' ] |
2、遍历切片

1 citys = ['beijing', 'tianjin', 'jizhou', 'shenzhen', 'guangzhou', 'shanghai'] 2 print("There is the north city list: ") 3 for city in citys[:3]: 4 print(city.title())

1 #!/usr/bin/env python 2 #-*- encoding: utf-8 -*- 3 citys = ['beijing', 'tianjin', 'jizhou', 'shenzhen', 'guangzhou', 'shanghai'] 4 citys_tmp = citys[:] 5 print("The new city list: ") 6 for city in citys_tmp: 7 print(city)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南