第三章: 列表简介

# 第三章: 列表简介
#  在本章和下一章中,你将学习列表是什么以及如何使用列表元素。列表让你能够在一个地方存储
# 成组的信息,其中可以只包含几个元素,也可以包含数百万个元素。
# 列表是新手可直接使用的最强大的Python功能之一,它融合了众多重要的编程概念。

# 在Python 中,用方括号([])来表示列表,并用逗号来分隔其中的元素。下面是一个简单的列表示例,这个列表包含几种自行车:

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




# 3.1.1 访问列表元素
# 列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或索引告诉Python即可。
# 要访问列表元素,可指出列表的名称,再指出元素的索引,并将其放在方括号内。
# bicycles = ['trek', 'cannondale', 'redline', 'specialized']
# print(bicycles[2])
# 可以使用方法title()让元素'trek'的格式更整洁
# print(bicycles[2].title())



# 3.1.2 索引从0 而不是1开始
# 在Python中,第一个列表元素的索引为0,而不是1.在大多数编程语言中都是如此,这与列表操作的底层实现相关。
# bicycles = ['trek', 'cannondale', 'redline', 'specialized']
# print(bicycles[1])
# print(bicycles[2])



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

# print(bicycles[-1])
# 这种语法很有用,因为你经常需要在不知道列表长度的情况下访问最后的元素。这种约定也适用于其他负数索引,例如,索引-2返回倒数第二个列表元素。



# 3.1.3 使用列表中的各个值
# 可像使用其他变量一样使用列表中的各个值。例如,你可以使用拼接根据列表中的值来创建消息。
'''
bicycles = ['trek', 'cannondale', 'redline', 'specialized']

message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)
'''



# 3.2 修改、添加和删除元素
# 你创建的大多数列表都将是动态的,这意味着列表创建后,将随着程序的运行增删元素。
# 3.2.1 修改列表元素
# 修改列表元素的语法与访问元素的语法类似。要修改列表元素,可指定列表名和要修改的元素索引,再指定该元素的新值。
'''
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles[0] = 'ducati'
print(motorcycles)
'''


# 3.2.2 在列表中添加元素
# 你可能出于众多原因要在列表中添加新元素,例如,你可能希望游戏中出现新的外星人、添加可视化数据或给网站添加新注册的用户。Python提供了多种在既有列表中添加新数据的方式。
# 1,  在列表中添加新元素时,最简单的方式是将元素附加到列表末尾。给列表附加元素时,它将添加到列表末尾。继续使用前一个示例中的列表,在其末尾添加新元素'ducati':
'''
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
'''


# 方法 append() 将元素'ducati'添加到了列表末尾,而不影响列表中的其他所有元素。
# 方法append() 让动态地创建列表易如反掌,例如,你可以先创建一个空列表,再使用一系列的append() 语句添加元素。下面来创建一个空列表,再在其中添加元 素'honda' 、'yamaha' 和'suzuki' :
'''
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')

print(motorcycles)
'''


# 3.2.3 从列表中删除元素
# 你经常需要从列表中删除一个或多个元素。
# 1,使用知道要删除的元素在列表中的位置,可使用del语句。
# 使用del语句将值从列表中删除后,你就无法再访问它了。
'''
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

del motorcycles[0]
print(motorcycles)
'''


# 2 ,使用方法 pop() 删除元素。
# 方法 pop() 可删除列表末尾的元素,并且让你能够接着使用它。术语弹出(POP)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶的元素。
'''
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
'''

# 方法 pop() 是怎么起作用的呢? 假设列表中的摩托车是按购买时间存储的,就可使用方法pop()打印一条消息,指出最后购买的是哪款摩托车。
'''
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print("The last motorcycle I owned was a " + last_owned.title() + ".")
'''


# 3.弹出列表中任何位置处的元素。
# 实际上,你可以使用pop()来删除列表中任何位置的元素,只需在括号中指定要删除的元素的索引即可。
'''
motorcycles = ['honda', 'yamaha', 'suziki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
'''

# 如果你不确定该使用del语句还是pop()方法,下面是一个简单的判断标准:如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del语句;如果你要在删除元素后还能继续使用它,就使用方法pop()

# 根据值删除元素
# 有时候,你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值,if 可使用方法remove():
'''
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
'''

# 使用remove()从列表中删除元素时,也可接着使用它的值。
'''
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")
'''
# 3.3 组织列表
# 在你创建的列表中,元素的排列顺序常常是无法预测的,因为你并非总能控制用户提供数据的顺序。这虽然在大多数情况下都是不可避免的,但你经常要以特定的顺序呈现信息。有时候,你希望保留列表元素最初的排列顺序,而有时候又需要调整排列顺序。python 提供了很多组织列表的方式,可根据具体情况选用。
# 3.3.1 使用方法 sort()对列表进行永久性排序。
# 使用方法 sort()让你能够较为轻松地对列表进行排序。
'''
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
'''
#  你还可以按与字母顺序相反的顺序排列列表元素,为此,只需向sort()方法传递参数reverse = True.同样,对列表的排列顺序的修改是永久性的。
'''
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
'''
# 3.3.2 使用函数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)
'''

# 注意:调用函数sorted() 后,列表元素的排列顺序并没有变。如果你要按与字母顺序相反的顺序显示列表,也可向函数sorted()传递参数reverse=True.
# 注意 在并非所有的值都是小写时,按字母顺序排列列表要复杂些。决定排序时,有多种解读大写字母的方式,要指定的排列顺序,可能比我们这里所做的要复杂。然而,大多数排序方式都时基于本节介绍的知识。

# 3.3.3 倒着打印列表
# 要反转列表元素的排列顺序,可使用方法 reverse()
'''
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
'''
# 注意,reverse()不是指按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序。

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

# 3.3.4 确认列表的长度
# 使用函数len()可快速获悉列表的长度。在下示例,列表包含4个元素,因此其长度为4。
'''
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
'''

练习题:

# 3-1 姓名: 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为names 。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
'''
names = ['Maxwell', 'John', 'Jack', 'Eve']
print(names)
'''

# 3-2 问候语: 问候语: 继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
'''
for i in names:
    print("hi %s"%i + ', Good Morning!')
'''
# 3-3 自己的列表: 自己的列表: 想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。
# 根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。
'''
tool_list = ['motorcycle', 'tank', 'bus11']

for m in tool_list:
    print("I would like to own a %s"%m)
'''
# 3-4 嘉宾名单 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用 这个列表打印消息,邀请这些人来与你共进晚餐。
'''
guest_of_honor = ['Eve', 'Linda', 'Christ', 'Maxwell']
print(guest_of_honor)

for i in guest_of_honor:
    print("Hi %s"%i + ", Could you please have dinner with me ?")
'''

'''
3-5 修改嘉宾名单 修改嘉宾名单 :你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。 以完成练习3-4时编写的程序为基础,在程序末尾添加一条print 语句,指出哪位嘉宾无法赴约。
修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
再次打印一系列消息,向名单中的每位嘉宾发出邀请。

guest_of_honor = ['Eve', 'Linda', 'Christ', 'Maxwell']
print(guest_of_honor)

for i in guest_of_honor:
    print("Hi %s"%i + ", Could you please have dinner with me ?")

print("Linda won't attend this dinner, due to she have another work need to follow up ! ")
guest_of_honor[1] = 'Christy'
print(guest_of_honor)

for i in guest_of_honor:
    print('Hi %s'%i + ", Would you mind having dinner with us?")
'''

'''
3-6 添加嘉宾 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。 以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print 语句,指出你找到了一个更大的餐桌。 使用insert() 将一位新嘉宾添加到名单开头。 使用insert() 将另一位新嘉宾添加到名单中间。 使用append() 将最后一位新嘉宾添加到名单末尾。
打印一系列消息,向名单中的每位嘉宾发出邀请。

guest_of_honor = ['Eve', 'Linda', 'Christ', 'Maxwell']
print(guest_of_honor)

for i in guest_of_honor:
    print("Hi %s"%i + ", Could you please have dinner with me ?")

print("Linda won't attend this dinner, due to she have another work need to follow up ! ")
guest_of_honor[1] = 'Christy'
print(guest_of_honor)

for i in guest_of_honor:
    print('Hi %s'%i + ", Would you mind having dinner with us?")

print("Hello %s"%i + ", Due to I book a huge table, Maybe we will invited more person to join us in this evening.")

guest_of_honor.insert(0, 'Nick')
print(guest_of_honor)
guest_of_honor.insert(3, 'John')
print(guest_of_honor)
guest_of_honor.append('Jack')
print(guest_of_honor)

for i in guest_of_honor:
    print("Hello %s"%i + ", Could you have a dinner with me ?")
    
'''
'''
3-7 缩减名单 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。 以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。 使用pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进 晚餐。
对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。 使用del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。 



guest_of_honor = ['Eve', 'Linda', 'Christ', 'Maxwell']
print(guest_of_honor)

for i in guest_of_honor:
    print("Hi %s"%i + ", Could you please have dinner with me ?")

print("Linda won't attend this dinner, due to she have another work need to follow up ! ")
guest_of_honor[1] = 'Christy'
print(guest_of_honor)

for i in guest_of_honor:
    print('Hi %s'%i + ", Would you mind having dinner with us?")

print("Hello %s"%i + ", Due to I book a huge table, Maybe we will invited more person to join us in this evening.")

guest_of_honor.insert(0, 'Nick')
print(guest_of_honor)
guest_of_honor.insert(3, 'John')
print(guest_of_honor)
guest_of_honor.append('Jack')
print(guest_of_honor)

for i in guest_of_honor:
    print("Hello %s"%i + ", Could you have a dinner with me ?")

print("Due to there is not enough tool to provide service what we want, so we just invite two person to join us !!! ")
count = 0
for i in guest_of_honor:
    guest_of_honor = guest_of_honor.pop()
    count += 0
    if count <= 2:
        break
print(guest_of_honor)
for i in guest_of_honor:
    print("Hi Luck Friends, It's my pleasure to be here to express gratitude there")
'''
'''
3-8 放眼世界 放眼世界 :想出至少5个你渴望去旅游的地方。
将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。
按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。
使用sorted() 按字母顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。
使用sorted() 按与字母顺序相反的顺序打印这个列表,同时不要修改它。
再次打印该列表,核实排列顺序未变。
使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。 
使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。 
使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。 
使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。 
'''
# tourist_attractions = ['England', 'Itly', 'America', 'France', 'Germany']
# print(tourist_attractions)
# print(sorted(tourist_attractions))
# print(tourist_attractions)
# print("使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。 ")
# tourist_attractions.reverse()
# print(tourist_attractions)
# print("使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。 ")
# tourist_attractions.reverse()
# print(tourist_attractions)
# print("使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。 ")
# tourist_attractions.sort()
# print(tourist_attractions)
# print("使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。 ")
# tourist_attractions.sort(reverse=True)
# print(tourist_attractions)
'''
3-9 晚餐嘉宾 晚餐嘉宾 :在完成练习3-4~练习3-7时编写的程序之一中,使用len() 打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。 
'''
guest_of_honor = ['Eve', 'Linda', 'Christ', 'Maxwell']
m = len(guest_of_honor)
print(' 我们总共邀请了%s'%m + ",与我们一起共度晚餐")

'''

发布了39 篇原创文章 · 获赞 4 · 访问量 3458

猜你喜欢

转载自blog.csdn.net/u011868279/article/details/103134868