Python第三章

# 3.1 列表是什么
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles) # 整个列表打印输出包括方括号

# 3.1.1 访问列表元素
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0]) # 列表元素打印输出不包括方括号喝引号
print(bicycles[0].title())

# 3.1.2 索引从0开始而不是1开始
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1]) # 打印输出cannondale
print(bicycles[3]) # 打印输出specialized
print(bicycles[-1]) # 特殊语法:通过将索引指定为-1,可以让python列表返回最后一个元素值,-2返回倒数第二个列表元素,-3返回倒数第三个列表元素,以此类推

# 3.1.3 使用列表中的各个值
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)

# 3-1 姓名列表(海贼王路飞阵营表),访问元素并打印出来
names = ['Monkey·D·Luffy', 'Roronoa Zoro', 'Nami', 'Usopp', 'Sanji', 'Tony-Tony Chopper', 'Nico Robin', 'Franky',
'Brook', 'Jinbe', 'Carrot']
print(names[0])
print(names[1])
print(names[2])
print(names[3])
print(names[4])
print(names[-6])
print(names[-5])
print(names[-4])
print(names[-3])
print(names[-2])
print(names[-1])

# 3-2 姓名列表,打印人名+问候语
print("Hello! " + names[0] + ".")
print("Hello! " + names[1] + ".")
print("Hello! " + names[2] + ".")
print("Hello! " + names[3] + ".")
print("Hello! " + names[4] + ".")
print("Hello! " + names[-6] + ".")
print("Hello! " + names[-5] + ".")
print("Hello! " + names[-4] + ".")
print("Hello! " + names[-3] + ".")
print("Hello! " + names[-2] + ".")
print("Hello! " + names[-1] + ".")

# 3-3 自己的通勤方式列表
transports = ['subway', 'bicycle', 'bus', 'plane', 'ship', 'honda motorcycle']
print("I would like to own a " + transports[-1].title() + ".")

# 3.2.1 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)

# 在列表中添加元素
# 1 在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
# 先创建一个空列表,再使用一系列的append()语句添加元素
motorcycles = []
motorcycles.append('honda')
motorcycles.append('ducati')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)

# 2 在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)

# 3.2.3 从列表中删除元素——使用del语句删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)

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

# 3.2.3 从列表中删除元素——使用pop()删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
poped_motorcycle = motorcycles.pop()
print(motorcycles)
print(poped_motorcycle)

# 3 弹出列表中任何位置处的元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')

# 4 根据值删除元素
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)

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-4 邀请晚宴名单 发出邀请
guests = ['Monkey·D·Luffy', 'Roronoa Zoro', 'Nami', 'Usopp', 'Sanji', 'Tony-Tony Chopper', 'Nico Robin', 'Franky',
'Brook', 'Jinbe', 'Carrot']
print("Welcome to the wedding, " + guests[0])
print("Welcome to the wedding, " + guests[1])
print("Welcome to the wedding, " + guests[2])
print("Welcome to the wedding, " + guests[3])
print("Welcome to the wedding, " + guests[4])
print("Welcome to the wedding, " + guests[-6])
print("Welcome to the wedding, " + guests[-5])
print("Welcome to the wedding, " + guests[-4])
print("Welcome to the wedding, " + guests[-3])
print("Welcome to the wedding, " + guests[-2])
print("Welcome to the wedding, " + guests[-1])

# 3-5 修改嘉宾名单,并增加一名被邀请人
guests = ['Monkey·D·Luffy', 'Roronoa Zoro', 'Nami', 'Usopp', 'Sanji', 'Tony-Tony Chopper', 'Nico Robin', 'Franky',
'Brook', 'Jinbe', 'Carrot']
a = guests[1]
guests.remove(a)
guests.append('Mary')
print(a + " cannot come.")
print("Welcome to the wedding, " + guests[0])
print("Welcome to the wedding, " + guests[1])
print("Welcome to the wedding, " + guests[2])
print("Welcome to the wedding, " + guests[3])
print("Welcome to the wedding, " + guests[4])
print("Welcome to the wedding, " + guests[-6])
print("Welcome to the wedding, " + guests[-5])
print("Welcome to the wedding, " + guests[-4])
print("Welcome to the wedding, " + guests[-3])
print("Welcome to the wedding, " + guests[-2])
print("Welcome to the wedding, " + guests[-1])

# 3-6 在列表头部、中部、末尾各添加一位嘉宾
guests = ['Monkey·D·Luffy', 'Roronoa Zoro', 'Nami', 'Usopp', 'Sanji', 'Tony-Tony Chopper', 'Nico Robin', 'Franky',
'Brook', 'Jinbe', 'Carrot']
guests.insert(0, 'Mike')
guests.insert(7, 'Nancy')
guests.insert(-1, 'June')
print("Welcome to the wedding, " + guests[0])
print("Welcome to the wedding, " + guests[1])
print("Welcome to the wedding, " + guests[2])
print("Welcome to the wedding, " + guests[3])
print("Welcome to the wedding, " + guests[4])
print("Welcome to the wedding, " + guests[5])
print("Welcome to the wedding, " + guests[6])
print("Welcome to the wedding, " + guests[7])
print("Welcome to the wedding, " + guests[-6])
print("Welcome to the wedding, " + guests[-5])
print("Welcome to the wedding, " + guests[-4])
print("Welcome to the wedding, " + guests[-3])
print("Welcome to the wedding, " + guests[-2])
print("Welcome to the wedding, " + guests[-1])

# 3-7 删除嘉宾以缩减名单
guests = ['guido van rossum', 'jack turner', 'lynn hill']
name1 = guests[0]
print(name1.title() + ", please come to the dinner.")
name2 = guests[1]
print(name2.title() + ", please come to the dinner.")
name3 = guests[2]
print(name3.title() + ", please come to the dinner.")
print("\nSorry!" + name2.title() + "can't make it to dinner.")
# Jack can't make it! Let's invite Gary instead.
del(guests[1])
guests.insert(1, 'gary snyder')
print(guests)
name1 = guests[0]
print(name1.title() + ", please come to the dinner.")
name2 = guests[1]
print(name2.title() + ", please come to the dinner.")
name3 = guests[2]
print(name3.title() + ", please come to the dinner.")
# We got a bigger table, so let's add some more people to the list.
print("\nWe got a bigger table!")
guests.insert(0, 'frida kahlo')
guests.insert(1, 'reinhold messor')
guests.append('elizabeth peratrovich')
print(guests)
name1 = guests[0]
print(name1.title() + ", please come to the dinner.")
name2 = guests[1]
print(name2.title() + ", please come to the dinner.")
name3 = guests[2]
print(name3.title() + ", please come to the dinner.")
name4 = guests[3]
print(name4.title() + ", please come to the dinner.")
name5 = guests[4]
print(name5.title() + ", please come to the dinner.")
name6 = guests[5]
print(name6.title() + ", please come to the dinner.")
# Oh no, the table won't arrive on time!
print("\nSorry, we can only invite two people to dinner.")
name1 = guests.pop()
print("Sorry, " + name1.title() + ". there is no room at the table.")
name2 = guests.pop()
print("Sorry, " + name2.title() + ". there is no room at the table.")
name3 = guests.pop()
print("Sorry, " + name3.title() + ". there is no room at the table.")
name4 = guests.pop()
print("Sorry, " + name4.title() + ". there is no room at the table.")
# There should be two people left. Let's invite them.
name1 = guests[0]
print(name1.title() + ", please come to the dinner.")
name2 = guests[1]
print(name2.title() + ", please come to the dinner.")
# Empty the list.
guests.pop() # 或者用del guests[0] 亦或是 del(guests[0])
guests.pop() # 或者用del guests[0] 亦或是 del(guests[0])
# Prove the list is empty.
print(guests)

# 3.3 组织列表
# 3.3.1 使用方法sort()对列表进行永久性排序,排序后新的列表按字母顺序相反的顺序排列,并且无法恢复原有顺序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
# 使用方法sort()对列表进行永久性排序,排序后新的列表按字母顺序排列,并且无法恢复原有顺序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)

# 3.3.2 使用函数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: ")
print(cars)

# 3.3.3 使用函数reverse()倒着打印列表
cars = ['bmw', 'audi', 'toyota', 'subaru']
print()
cars.reverse() # 将列表翻转,永久性排序,要恢复至原有顺序需要再次调用reverse函数
print(cars)

# 3.3.4 使用函数len()确定列表长度
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))

# 3.4 使用列表是避免索引错误
motorcycles = ['honda', 'yamaha', 'suzuki']
# print(motorcycles[3]) # 列表长为3,没有第四个元素运行会报错
# 当列表motorcycles = [] 时,print(motorcycles[-1]) 即运行下面两句会导致索引错误
# motorcycles = []
# print(motorcycles[-1])
print(motorcycles[-1])

# 3-8 放眼世界:按要求打印输出旅游地列表中的元素
places = ['beijing', 'paris', 'new york', 'nanjing', 'guangzhou']
print("Original order: ")
print(places)
print("\nAlphabetical: ")
print(sorted(places))
print("\nOriginal order: ")
print(places)
print("\nReverse Alphabetical order: ")
print(sorted(places, reverse=True))
print("\nOriginal order: ")
print(places)
print("\nReversed: ")
places.reverse()
print(places)
print("\nOriginal order: ")
places.reverse()
print(places)
print("\nAlphabetical order: ")
places.sort()
print(places)
print("\nReverse Alphabetical order: ")
places.sort(reverse=True)
print(places)

# 3-9 使用函数len()指出所邀请的人数
guests = ['Monkey·D·Luffy', 'Roronoa Zoro', 'Nami', 'Usopp', 'Sanji', 'Tony-Tony Chopper', 'Nico Robin', 'Franky',
'Brook', 'Jinbe', 'Carrot']
print(guests)
print(len(guests))

posted @ 2020-06-08 11:23  燃烧信仰TopTan  阅读(198)  评论(0)    收藏  举报