Python Crush Course - Chapter 3

列表

列表由一系列按特定顺序排列的元素组成,列表中可以包含任何元素。
在Python中用方括号([])来表示列表,并用逗号来分隔其中的元素。

bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized']
print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']

访问列表元素

列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或索引告诉Python即可。要访问列表元素,可指出列表的名称,再指出元素的索引,并将其放在方括号内。

bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized']
print(bicycles[0])
trek
bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized']
print(bicycles[0].title())
Trek

##索引从0而不是1开始
Python中,第一个列表元素的索引为0,而不是1.

bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized']
print(bicycles[3])
print(bicycles[2].title())
specialized
Redline

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

bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized']
print(bicycles[-1])
specialized

这种约定也适合其他负数索引,例如索引-2返回倒数第二个列表元素,索引-3返回倒数第三个列表元素,依此类推。

使用列表中的各个值

可以像使用其他变量一样使用列表中的各个值。

bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized']
message = "My first bike was a " + bicycles[0].title() + "."
print(message)
My first bike was a Trek.

练习题

  1. 将一些朋友的姓名储存在列表中,并将其命名为names。依次访问列表中的每个元素,从而将每个朋友的姓名都打印出来。
names = ['eric' , 'nick' , 'mike' , 'joshua']
print(names[0].title())
print(names[1].title())
print(names[2].title())
print(names[3].title())
Eric
Nick
Mike
Joshua
  1. 继续使用上题中的列表,但不打印每个朋友的姓名,而为每个人打印一条小希。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。继续使用上题中的列表,但不打印每个朋友的姓名,而为每个人打印一条小希。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names = ['eric' , 'nick' , 'mike' , 'joshua']
message = ", good morning!"
print(names[0].title() + message)
print(names[1].title() + message)
print(names[2].title() + message)
print(names[3].title() + message)
Eric, good morning!
Nick, good morning!
Mike, good morning!
Joshua, good morning!
  1. 创建一个包含多种通勤方式的列表。根据列表打印一系列有关通勤方式的宣言。
vehicles = ["Benz car" , "Honda motorcycle" , "Trek bicycle"]
message = "I would like to own a "
print(message + vehicles[0] + "!")
print(message + vehicles[1] + "!")
print(message + vehicles[2] + "!")
I would like to own a Benz car!
I would like to own a Honda motorcycle!
I would like to own a Trek bicycle!

修改、添加和删除元素

大多数列表都是动态的,这意味着列表创建后,将随着程序的运行增删元素。

修改列表元素

要修改列表中的元素,可指定列表名和要修改的元素的索引,再指定该元素的新值。

motorcycles = ['honda' , 'yamaha' , 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
['honda' , 'yanaha' , 'suzuki']
['ducati' , 'yamaha' , 'suzuki']

在列表中添加元素

在列表末尾添加元素

在列表中添加新元素时,最简单的方式是将元素附加到列表末尾。给列表附加元素时,它将添加到列表末尾。

motorcycles = ['honda' , 'yamaha' , 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']

**方法append()**将元素‘ducati’添加到列表末尾,而不影响列表中的其他所有元素。

motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
['honda' , 'yamaha' , 'suzuki']

在列表中插入元素

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

motorcycles = ['honda' , 'yamaha' , 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
['ducati' , 'honda' , 'yamaha' , 'suzuki']

从列表中删除元素

使用del语句删除元素

如果知道要删除的元素在列表中的位置,可使用del语句。

motorcycles = ['honda' , 'yamaha' , 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
['honda' , 'yamaha' , 'suzuki']
['yamaha' , 'suzuki']

使用del可删除任何位置的列表元素,条件是知道其索引。

motorcycles = ['honda' , 'yamaha' , 'suzuki']
del motorcycles[1]
print(motorcycles)
['honda' , 'suzuki']

在这两个示例中,使用del语句将值从列表中删除后,就无法再访问它了。

使用方法pop()删除元素

方法pop()可以删除列表末尾的元素,并让你能够接着使用它的值。

motorcycles = ['honda' , 'yamaha' , 'suzuki']
print(motorcycles)
popped_motorcycles = motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
['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

如果要删除一个元素,且不再以任何方式使用它,就是用del语句;如果你要在删除元素后还能继续使用它,就是用方法pop()。

根据值删除元素

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

motorcycles = ['honda' , 'yamaha' , 'suzuki' , 'ducati']
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print("\nA " + too_expensive + " is too expensive for me.")
['honda', 'yamaha', 'suzuki']

A ducati is too expensive for me.

练习题

  1. 3-4
guests = ['nicky' , 'walter' , 'sophie' , 'mike']
print("I want to invite " + guests[0].title() + " to come to my party.")
print("I want to invite " + guests[1].title() + " to come to my party.")
print("I want to invite " + guests[2].title() + " to come to my party.")
print("I want to invite " + guests[3].title() + " to come to my party.")
I want to invite Nicky to come to my party.
I want to invite Walter to come to my party.
I want to invite Sophie to come to my party.
I want to invite Mike to come to my party.
  1. 3-5
guests = ['nicky' , 'walter' , 'sophie' , 'mike']
absent_guests = guests.pop()
print(absent_guests.title() + " will not come to this party.")
guests.append('josh')
print("I want to invite " + guests[0].title() + " to come to my party.")
print("I want to invite " + guests[1].title() + " to come to my party.")
print("I want to invite " + guests[2].title() + " to come to my party.")
print("I want to invite " + guests[3].title() + " to come to my party.")
Mike will not come to this party.
I want to invite Nicky to come to my party.
I want to invite Walter to come to my party.
I want to invite Sophie to come to my party.
I want to invite Josh to come to my party.
  1. 3-6
guests = ['nicky' , 'walter' , 'sophie' , 'mike']
absent_guests = guests.pop()
print(absent_guests.title() + " will not come to this party.")
guests.append('josh')
guests.insert(0 , 'lucy')
guests.insert(3 , 'elizabeth')
guests.append('lee')
print("I want to invite " + guests[0].title() + " to come to my party.")
print("I want to invite " + guests[1].title() + " to come to my party.")
print("I want to invite " + guests[2].title() + " to come to my party.")
print("I want to invite " + guests[3].title() + " to come to my party.")
print("I want to invite " + guests[4].title() + " to come to my party.")
print("I want to invite " + guests[5].title() + " to come to my party.")
print("I want to invite " + guests[6].title() + " to come to my party.")
Mike will not come to this party.
I want to invite Lucy to come to my party.
I want to invite Nicky to come to my party.
I want to invite Walter to come to my party.
I want to invite Elizabeth to come to my party.
I want to invite Sophie to come to my party.
I want to invite Josh to come to my party.
I want to invite Lee to come to my party.
  1. 3-7
guests = ['nicky' , 'walter' , 'sophie' , 'mike']
absent_guests = guests.pop()
print(absent_guests.title() + " will not come to this party.")
guests.append('josh')
guests.insert(0 , 'lucy')
guests.insert(3 , 'elizabeth')
guests.append('lee')
cancel_guests = guests.pop()
print(cancel_guests.title() + " , I'm so sorry I can't invite you.")
cancel_guests = guests.pop()
print(cancel_guests.title() + " , I'm so sorry I can't invite you.")
cancel_guests = guests.pop()
print(cancel_guests.title() + " , I'm so sorry I can't invite you.")
cancel_guests = guests.pop()
print(cancel_guests.title() + " , I'm so sorry I can't invite you.")
cancel_guests = guests.pop()
print(cancel_guests.title() + " , I'm so sorry I can't invite you.")
print(guests[0].title() + " , I still invite you to this party.")
print(guests[1].title() + " , I still invite you to this party.")
del guests[1]
del guests[0]
print(guests)
Mike will not come to this party.
Lee , I'm so sorry I can't invite you.
Josh , I'm so sorry I can't invite you.
Sophie , I'm so sorry I can't invite you.
Elizabeth , I'm so sorry I can't invite you.
Walter , I'm so sorry I can't invite you.
Lucy , I still invite you to this party.
Nicky , I still invite you to this party.
[]

组织列表

使用方法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']

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

要保留列表原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted()。函数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']

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

倒着打印列表

要反转列表元素的排列顺序,可使用方法reverse()。

cars = ['bmw' , 'audi' , 'toyota' , 'subaru']
print(cars)

cars.reverse()
print(cars)
['bmw' , 'audi' , 'toyota' , 'subaru']
['subaru' , 'toyota' , 'bmw' , 'audi']

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

确定列表的长度

使用函数len()可快速获悉列表的长度。

cars = ['bmw' , 'audi' , 'toyota' , 'subaru']
len(cars)
4

练习题

3-8

places = ['beijing' , 'shanghai' , 'guangzhou' , 'hangzhou' , 'xianggang']
print(places)
print(sorted(places))
print(places)
print(sorted(places,reverse = True))
print(places)
print(places.reverse())
['beijing', 'shanghai', 'guangzhou', 'hangzhou', 'xianggang']
['beijing', 'guangzhou', 'hangzhou', 'shanghai', 'xianggang']
['beijing', 'shanghai', 'guangzhou', 'hangzhou', 'xianggang']
['xianggang', 'shanghai', 'hangzhou', 'guangzhou', 'beijing']
['beijing', 'shanghai', 'guangzhou', 'hangzhou', 'xianggang']
None
places = ['beijing' , 'shanghai' , 'guangzhou' , 'hangzhou' , 'xianggang']
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse = True)
print(places)
['xianggang', 'hangzhou', 'guangzhou', 'shanghai', 'beijing']
['beijing', 'shanghai', 'guangzhou', 'hangzhou', 'xianggang']
['beijing', 'guangzhou', 'hangzhou', 'shanghai', 'xianggang']
['xianggang', 'shanghai', 'hangzhou', 'guangzhou', 'beijing']

3-9

guests = ['nicky' , 'walter' , 'sophie' , 'mike']
absent_guests = guests.pop()
print(absent_guests.title() + " will not come to this party.")
guests.append('josh')
guests.insert(0 , 'lucy')
guests.insert(3 , 'elizabeth')
guests.append('lee')
len(guests)
Mike will not come to this party.
7

3-10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值