- 2. 列表介绍
- 2.1 列表是什么
- 2.1.1 访问列表元素
- 2.1.2 索引从0而不是1开始
- 2.2 修改、添加和删除元素
- 2.2.1 修改列表元素
- 2.2.2 在列表中添加元素
- 2.2.3 从列表中删除元素
- 2.3 组织列表
- 2.3.1 使用方法sort() 对列表进行 永久性排序
- 2.3.2 使用函数sorted() 对列表进行 临时排序
- 2.3.3 倒着打印列表
- 2.3.4 确定列表的长度
- 3. 操作列表
- 3.1 遍历整个列表
- 3.2 创建数值列表
- 3.2.1 使用函数range()
- 3.2.2 使用range() 创建数字列表
- 3.2.3 对数字列表执行简单的统计计算
- 3.2.4 列表解析
- 3.3 使用列表的一部分
- 3.3.1 切片
- 3.3.2 复制列表
用方括号([] )来表示列表,并用逗号来分隔其中的元素。列表 由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
# ['trek', 'cannondale', 'redline', 'specialized']
2.1.1 访问列表元素只需将该元素的位置
或索引
告诉Python即可。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
# trek
# 可使用方法title() 让元素'trek' 的格式更整洁:
print(bicycles[0].title())
# Trek
2.1.2 索引从0而不是1开始第一个列表元素的索引为0
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1])
print(bicycles[3])
# cannondale
# specialized
将索引指定为-1
,可让Python返回最后一个列表元素
:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])
# specialized
2.2 修改、添加和删除元素
2.2.1 修改列表元素motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
# ['honda', 'yamaha', 'suzuki']
motorcycles[0] = 'ducati'
print(motorcycles)
# ['ducati', 'yamaha', 'suzuki']
2.2.2 在列表中添加元素- 在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
# ['honda', 'yamaha', 'suzuki']
motorcycles.append('ducati')
print(motorcycles)
# ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles2 = []
motorcycles2.append('honda')
motorcycles2.append('yamaha')
motorcycles2.append('suzuki')
print(motorcycles2)
# ['honda', 'yamaha', 'suzuki', 'ducati']
- 在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati') ❶
print(motorcycles)
# ['ducati', 'honda', 'yamaha', 'suzuki']
2.2.3 从列表中删除元素- 使用del 语句删除元素,删除后,你就无法再访问它了。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
# ['honda', 'yamaha', 'suzuki']
del motorcycles[0]
print(motorcycles)
# ['yamaha', 'suzuki']
del 可删除任何位置处的列表元素,条件是知道其索引。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)
# ['honda', 'yamaha', 'suzuki']
# ['honda', 'suzuki']
- 使用方法pop() 删除末尾元素
方法pop() 可删除列表末尾
的元素,并让你能够接着使用它。术语弹出 (pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
# ['honda', 'yamaha', 'suzuki']
# ['honda', 'yamaha']
# suzuki
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print("The last motorcycle I owned was a " + last_owned.title() + ".")
# The last motorcycle I owned was a Suzuki.
- 弹出列表中任何位置处的元素
pop()+索引
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.
- 根据值删除元素
方法remove()
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
# ['honda', 'yamaha', 'suzuki', 'ducati']
# ['honda', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] ❶
print(motorcycles)
too_expensive = 'ducati' ❷
motorcycles.remove(too_expensive) ❸
print(motorcycles)
print("\nA " + too_expensive.title() + " is too expensive for me.") # \n 回车
# ['honda', 'yamaha', 'suzuki', 'ducati']
# ['honda', 'yamaha', 'suzuki']
# A Ducati is too expensive for me.
2.3 组织列表
2.3.1 使用方法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']
2.3.2 使用函数sorted() 对列表进行 临时排序调用函数sorted() 后,列表元素的排列顺序并没有变。如果你要按与字母顺序相反的顺序显示列表,也可向函数sorted() 传递参数reverse=True
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)
output:
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'] # 原始顺序
2.3.3 倒着打印列表reverse() ,永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,为此只需对列表再次调用reverse() 即可。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
# ['bmw', 'audi', 'toyota', 'subaru']
# ['subaru', 'toyota', 'audi', 'bmw']
2.3.4 确定列表的长度len()
>>>cars = ['bmw', 'audi', 'toyota', 'subaru']
>>>len(cars)
4
3. 操作列表
3.1 遍历整个列表for
循环
magicians = ['alice', 'david', 'carolina'] ❶
for magician in magicians: ❷
print(magician) ❸
alice
david
carolina
3.2 创建数值列表
3.2.1 使用函数range()for value in range(1,5):
print(value)
# 输出
1
2
3
4
for value in range(1,6):
print(value)
1
2
3
4
5
3.2.2 使用range() 创建数字列表numbers = list(range(1,6))
print(numbers)
# [1, 2, 3, 4, 5]
even_numbers = list(range(2,11,2))
print(even_numbers)
# [2, 4, 6, 8, 10]
3.2.3 对数字列表执行简单的统计计算>>>digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>>min(digits)
0
>>>max(digits)
9
>>>sum(digits)
45
3.2.4 列表解析列表解析 将for 循环和创建新元素的代码合并成一行,并自动附加新元素。
squares = [value**2 for value in range(1,11)]
print(squares)
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3.3 使用列表的一部分
3.3.1 切片players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) ❶
['charles', 'martina', 'michael']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
['martina', 'michael', 'florence']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
['charles', 'martina', 'michael', 'florence']
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])
['michael', 'florence', 'eli']
print(players[-3: -1])
['michael', 'florence']
3.3.2 复制列表要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:]
)。这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表。
my_foods = ['pizza', 'falafel', 'carrot cake'] ❶
friend_foods = my_foods[:] ❷
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
输出:
My favorite foods are:
['pizza', 'falafel', 'carrot cake']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
另外
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:] ❶
my_foods.append('cannoli') ❷
friend_foods.append('ice cream') ❸
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
# My favorite foods are:
# ['pizza', 'falafel', 'carrot cake', 'cannoli'] ❹
# My friend's favorite foods are:
# ['pizza', 'falafel', 'carrot cake', 'ice cream'] ❺
错误方法:
my_foods = ['pizza', 'falafel', 'carrot cake']
#这行不通
friend_foods = my_foods ❶
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
输出:
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
本文题目:python编程从入门到实践2——列表-创新互联
路径分享:http://scgulin.cn/article/dddscp.html