对应书目:Python编程:从入门到实践
列表简介(第三章)
要理解新的编程概念,最佳的方法是尝试在程序中使用它们
- Python通过将索引指定为-1,可以让Python返回最后一个列表元素
动手试一试
3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为 names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来
my_friends = ['tom',"jerry"]
print(my_friends[0].title()+" "+my_friends[1].title())
3-2 问候语: 继续使用练习 3-1 中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名
my_friends = ['tom',"jerry"]
message = "nice to meet you!"
print(message + " " + my_friends[0].title())
print(message + " " + my_friends[1].title())
3-3 自己的列表: 想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”
traffic_tools = ['car','honda motorcycle']
message = "I would like to own a"
print(message + " " + traffic_tools[0].title())
print(message + " " + traffic_tools[1].title())
- .append()将元素添加到列表末尾
- .insert(索引,值)
- del 列表[索引]表示删除元素,永久删除,只知道要删除的元素的索引
- 列表.pop()删除末尾元素,列表像一个栈,出栈
- 列表.pop(索引)出栈任何位置元素
- 列表.remove(元素)只知道要删除元素的值
动手试一试
3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少 3 个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐
friends = ['tom','jerry','merry']
print(friends[0]+" " + friends[1] + " " + friends[-1])
3-5 修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾
以完成练习 3-4 时编写的程序为基础,在程序末尾添加一条 print 语句,指出哪位嘉宾无法赴约
修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名
再次打印一系列消息,向名单中的每位嘉宾发出邀请
friends = ['tom','jerry','merry']
print("Welcome"+" " + friends[0].title()+" " + friends[1].title() + " " + friends[-1].title())
name = friends.pop(-1)
print("But "+ name.title() + " can't keep the appointment")
friends.append('michael')
print(friends)
print("Welcome"+" " + friends[0].title()+" " + friends[1].title() + " " + friends[-1].title())
3-6 添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
以完成练习 3-4 或练习 3-5 时编写的程序为基础,在程序末尾添加一条 print 语句,指出你找到了一个更大的餐桌。
使用 insert()将一位新嘉宾添加到名单开头
使用 insert()将另一位新嘉宾添加到名单中间
使用 append()将最后一位新嘉宾添加到名单末尾
打印一系列消息,向名单中的每位嘉宾发出邀请
friends = ['tom','jerry','merry']
print("Welcome"+" " + friends[0].title()+" " + friends[1].title() + " " + friends[-1].title())
name = friends.pop(-1)
print("But "+ name.title() + " can't keep the appointment")
friends.append('michael')
print("I found a bigger table")
friends.insert(0,'John')
friends.insert(2,'Aha')
friends.append('Gary')
print(friends)
print("Welcome"+" " + friends[0].title() + " " + friends[1].title() + " " + friends[-3].title() + " "
+ friends[-2].title() + " " + friends[-1].title())
3-7 缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾
以完成练习 3-6 时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息
使用 pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐
对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列
使用 del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的
friends = ['tom','jerry','merry']
print("Welcome"+" " + friends[0].title()+" " + friends[1].title() + " " + friends[-1].title())
name = friends.pop(-1)
print("But "+ name.title() + " can't keep the appointment")
friends.append('michael')
print("I found a bigger table")
friends.insert(0,'John')
friends.insert(2,'Aha')
friends.append('Gary')
print(friends)
print("We can only invite"+" " + friends[1].title()+" " + friends[3].title())
print("Sorry! " + friends.pop(0))
print("Sorry! " + friends.pop())
print("Sorry! " + friends.pop(1))
print("Sorry! " + friends.pop(2))
print("Welcome"+" " + friends[0].title()+" " + friends[1].title())
del friends[0]
del friends[0]
print(friends)
- .sort()按字母表顺序排序
- .sort(reverse-True)按字母表顺序相反的顺序排序
- sorted(列表)要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们
- 如果你要按与字母顺序相反的顺序显示列表,也可sorted(reverse=True)
- .reverse()反转列表元素排列顺序
- len(列表)获取列表长度
动手试一试
3-8 放眼世界:想出至少 5 个你渴望去旅游的地方
将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的
按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python 列表
使用 sorted()按字母顺序打印这个列表,同时不要修改它
再次打印该列表,核实排列顺序未变
使用 sorted()按与字母顺序相反的顺序打印这个列表,同时不要修改它
再次打印该列表,核实排列顺序未变
使用 reverse()修改列表元素的排列顺序。 打印该列表, 核实排列顺序确实变了
使用 reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序
使用 sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了
使用 sort()修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了
attractions = ['Ming tombs','Forbidden City','Great Wall','Terra Cotta Warriors','Mount tai']
print("原顺序:"+'、'.join(attractions)) #'、'.join(attractions)将列表转化为字符串形式,前面的连接形式
print("按字母顺序打印这个列表:"+'、'.join(sorted(attractions)))
print("核实sorted(attractions)使原来排列顺序未变:"+'、'.join(attractions))
print(sorted(attractions,reverse=True))
print("核实sorted(attractions,reverse=True)使原来排列顺序未变:"+'、'.join(attractions))
attractions.reverse()
print("按与元素顺序相反的顺序打印:"+'、'.join(attractions))
attractions.reverse()
print("核实已恢复到原来的排列顺序:"+'、'.join(attractions))
attractions.sort()
print("sort()修改该列表,使其元素按字母顺序排列:"+'、'.join(attractions))
print("核实排列顺序确实变了:"+'、'.join(attractions))
attractions.sort(reverse=True)
print("使其元素按与字母顺序相反的顺序排列:"+'、'.join(attractions))
print("核实排列顺序确实变了:"+'、'.join(attractions))
3-9 晚餐嘉宾:在完成练习 3-4~练习 3-7 时编写的程序之一中,使用 len()打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐
friends = ['tom','jerry','merry']
print("Welcome"+" " + friends[0].title()+" " + friends[1].title() + " " + friends[-1].title())
name = friends.pop(-1)
print("But "+ name.title() + " can't keep the appointment")
friends.append('michael')
print("I found a bigger table")
friends.insert(0,'John')
friends.insert(2,'Aha')
friends.append('Gary')
print(friends)
print("We can only invite"+" " + friends[1].title()+" " + friends[3].title())
print("Sorry! " + friends.pop(0))
print("Sorry! " + friends.pop())
print("Sorry! " + friends.pop(1))
print("Sorry! " + friends.pop(2))
print("Welcome"+" " + friends[0].title()+" " + friends[1].title())
print("All people = "+str(len(friends)))
3-10 尝试使用各个函数:想想可存储到列表中的东西,如山岳、河流、国家、城市、语言或你喜欢的任何东西。编写一个程序,在其中创建一个包含这些元素的列表,然后,对于本章介绍的每个函数,都至少使用一次来处理这个列表
attractions = ['B','C','A','E','D']
print("原顺序:"+'、'.join(attractions)) #'、'.join(attractions)将列表转化为字符串形式,前面的连接形式
print("按字母顺序打印这个列表:"+'、'.join(sorted(attractions)))
print("核实sorted(attractions)使原来排列顺序未变:"+'、'.join(attractions))
print(sorted(attractions,reverse=True))
print("核实sorted(attractions,reverse=True)使原来排列顺序未变:"+'、'.join(attractions))
attractions.reverse()
print("按与元素顺序相反的顺序打印:"+'、'.join(attractions))
attractions.reverse()
print("核实已恢复到原来的排列顺序:"+'、'.join(attractions))
attractions.sort()
print("sort()修改该列表,使其元素按字母顺序排列:"+'、'.join(attractions))
print("核实排列顺序确实变了:"+'、'.join(attractions))
attractions.sort(reverse=True)
print("使其元素按与字母顺序相反的顺序排列:"+'、'.join(attractions))
print("核实排列顺序确实变了:"+'、'.join(attractions))