2.16 python入门到实践 第三章列表简介

 
 
  1 bicycles = ['trek','connondale','redline','specialized']
  2 print(bicycles[0])
  3 
  4 bicycles = ['trek','connondale','redline','specialized']
  5 print(bicycles[0].title())#首字母大写
  6 
  7 # 索引从0而不是1开始,-3为倒数第三个
  8 bicycles = ['trek','connondale','redline','specialized']
  9 print(bicycles[0])
 10 print(bicycles[3])
 11 print(bicycles[-3])
 12 print(bicycles[-1])
 13 
 14 bicycles = ['trek','connondale','redline','specialized']
 15 message = "My bicycle was a " + bicycles[0].title() + "."
 16 print(message)
 17 # >>>My bicycle was a Trek.
 18 
 19 #3-1练习
 20 names = ['phyllis','momoe','erzi','ergou','shiitakeimoto','dudu']
 21 message = names[0].title() + "," + "Happy new yaer!"
 22 print(message)
 23 car = ['bmw','benz','audi','maserati']
 24 message2 = "I would like to own a " + car[3].title() + "."
 25 print(message2)
 26 
 27 #给列表添加元素
 28 girls = []
 29 girls.append('phyllis')
 30 girls.append('momoe')
 31 girls.append('ergou')
 32 girls.append('erzi')#只能单个添加
 33 print(girls)
 34 
 35 # 在列表中插入元素 .insert指定位置 append在列表最后
 36 they = ['momoe','ergou','erzi','shiitakeimoto']
 37 they.insert(0,'phyllis')#先位置后元素
 38 they.insert(3,'dudu')#添加在3前面
 39 print(they)
 40 #列表删除元素 del
 41 del they[5]
 42 print(they)
 43 
 44 #术语弹出 (pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。如果你不确定该使用del 语句还是pop() 方法,下面是一个简单的判断标准:
 45 #如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del 语句;如果你要在删除元素后还能继续使用它,就使用方法pop() 。
 46 they = ['momoe','ergou','erzi','shiitakeimoto']
 47 popped_they = they.pop()
 48 print(they)
 49 print(popped_they)
 50 #
 51 # #如果你只知道要删除的元素的值(位置不可),可使用方法remove()
 52 motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
 53 print(motorcycles)
 54 too_expensive = 'ducati'
 55 motorcycles.remove(too_expensive)
 56 print(motorcycles)
 57 print("\nA " + too_expensive.title() + " is too expensive for me.")
 58 # #注意  方法remove() 只删除第一个指定的值。如果要删除的值可能在列表中出现多次,
 59 # 就需要使用循环来判断是否删除了所有这样的值。你将在第7章学习如何这样做。
 60 
 61 # 3-4 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用
 62 # 这个列表打印消息,邀请这些人来与你共进晚餐。
 63 # 3-5 修改嘉宾名单 :你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。
 64 # 以完成练习3-4时编写的程序为基础,在程序末尾添加一条print 语句,指出哪位嘉宾无法赴约。
 65 # 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
 66 # 再次打印一系列消息,向名单中的每位嘉宾发出邀请。
 67 # 3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
 68 # 以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print 语句,指出你找到了一个更大的餐桌。
 69 # 使用insert() 将一位新嘉宾添加到名单开头。
 70 # 使用insert() 将另一位新嘉宾添加到名单中间。
 71 # 使用append() 将最后一位新嘉宾添加到名单末尾。
 72 # 打印一系列消息,向名单中的每位嘉宾发出邀请。
 73 # 3-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
 74 # 以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
 75 # 使用pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进
 76 # 晚餐。
 77 # 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
 78 # # 使用del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
 79 # len
 80 #
 81 #练习3-4
 82 meal_with = ['phyllis','ergou','erzi','momoe']
 83 n = 0
 84 x = len(meal_with)
 85 while n < x:#列表元素个数代码 #问题 n <= x 时报错 回头看
 86     invitation = meal_with[n].title() + ",Please have a meal with us."
 87     print(invitation)
 88     n += 1
 89 popped_meal_with =meal_with.pop(0)#问题 元素的位置有误
 90 regret = meal_with.pop(0).title() + " can't comes."#.pop此处已执行第二次,下文订正
 91 print(meal_with)
 92 print(regret)
 93 revisal = "以下为订正后结果"
 94 FBI_warning = revisal.center(30,'*')
 95 print(FBI_warning)
 96 meal_with2 = ['phyllis','ergou','erzi','momoe']
 97 print(meal_with2)
 98 popped_meal_with2 =meal_with2.pop(3)#问题 元素的位置有误
 99 regret2 = popped_meal_with2.title() + " can't comes.\n" + "*" * 38
100 print(meal_with2)
101 print(regret2)
102 
103 print("\nNow invite other girls.")
104 meal_with2.append('shiitakeimoto')
105 print(meal_with2)
106 print('Now send invitations\n')
107 n2 = 0
108 x2 = len(meal_with2)
109 while n2 < x2:#列表元素个数代码
110     invitation2 = meal_with2[n2].title() + ",Please have a meal with us."
111     print(invitation2)
112     n2 += 1
113 
114 #3-6 more girls
115 meal_with2 = ['shiitakeimoto', 'ergou', 'erzi',"dudu"]
116 new_message = "Now I found one bigger table.Let me invite more girls!\n" + '*' * 54 + "\n"
117 print(new_message)
118 meal_with2.insert(0,'phyllis')
119 meal_with2.insert(-1,"soso")
120 number_of_people = len(meal_with2)
121 z = number_of_people/2 #结果如何输出为int
122 meal_with2.insert(3,"harashima")#问题 如何取列表元素个数的中位数
123 n3 = 0
124 x3 = number_of_people
125 while n3 <= x3: #问题 为何此时用 <= 不报错
126     invitation3 = meal_with2[n3].title() + " Please have a meal with us."
127     print(invitation3)
128     n3 += 1
129 
130 # ['dudu',  'ergou', 'harashima', 'erzi', 'soso','phyllis', 'shiitakeimoto']
131 new_message2 = '\n\nUnfortunatlly.I odered table can\'t arrive today.So I only can invite 2 persons.I\'m so sorry to you\n'
132 print(new_message2)
133 print(meal_with2)
134 cancel = len(meal_with2)
135 while cancel > 3:
136     cancel = len(meal_with2)
137     popped_meal = meal_with2.pop()
138     sorry = popped_meal.title() + ",I'm so sorry."
139     print(sorry)
140 go_with1 = '\n' + meal_with2[0].title() + ',Please have a meal with me.'
141 go_with2 = meal_with2[1].title() + ',Please have a meal with me.'
142 print(go_with1)
143 print(go_with2)
144 # popped_they = they.pop()
145 # print(they)
146 # print(popped_they)
147 del meal_with2[0]
148 del meal_with2[0]
149 print(meal_with2)
 
  
 
 
# bicycles = ['trek','connondale','redline','specialized']
# print(bicycles[0])

# bicycles = ['trek','connondale','redline','specialized']
# print(bicycles[0].title())#首字母大写

# 索引从0而不是1开始,-3为倒数第三个
# bicycles = ['trek','connondale','redline','specialized']
# print(bicycles[0])
# print(bicycles[3])
# print(bicycles[-3])
# print(bicycles[-1])

# bicycles = ['trek','connondale','redline','specialized']
# message = "My bicycle was a " + bicycles[0].title() + "."
# print(message)
# >>>My bicycle was a Trek.

#3-1练习
# names = ['phyllis','momoe','erzi','ergou','shiitakeimoto','dudu']
# message = names[0].title() + "," + "Happy new yaer!"
# print(message)
# car = ['bmw','benz','audi','maserati']
# message2 = "I would like to own a " + car[3].title() + "."
# print(message2)

#给列表添加元素
# girls = []
# girls.append('phyllis')
# girls.append('momoe')
# girls.append('ergou')
# girls.append('erzi')#只能单个添加
# print(girls)

# 在列表中插入元素 .insert指定位置 append在列表最后
# they = ['momoe','ergou','erzi','shiitakeimoto']
# they.insert(0,'phyllis')#先位置后元素
# they.insert(3,'dudu')#添加在3前面
# print(they)
# #列表删除元素 del
# del they[5]
# print(they)

#术语弹出 (pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。如果你不确定该使用del 语句还是pop() 方法,下面是一个简单的判断标准:
#如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del 语句;如果你要在删除元素后还能继续使用它,就使用方法pop() 。
# they = ['momoe','ergou','erzi','shiitakeimoto']
# popped_they = they.pop()
# print(they)
# print(popped_they)
#
# #如果你只知道要删除的元素的值(位置不可),可使用方法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.")
# # #注意  方法remove() 只删除第一个指定的值。如果要删除的值可能在列表中出现多次,
# # 就需要使用循环来判断是否删除了所有这样的值。你将在第7章学习如何这样做。

# 3-4 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用
# 这个列表打印消息,邀请这些人来与你共进晚餐。
# 3-5 修改嘉宾名单 :你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。
# 以完成练习3-4时编写的程序为基础,在程序末尾添加一条print 语句,指出哪位嘉宾无法赴约。
# 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
# 再次打印一系列消息,向名单中的每位嘉宾发出邀请。
# 3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
# 以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print 语句,指出你找到了一个更大的餐桌。
# 使用insert() 将一位新嘉宾添加到名单开头。
# 使用insert() 将另一位新嘉宾添加到名单中间。
# 使用append() 将最后一位新嘉宾添加到名单末尾。
# 打印一系列消息,向名单中的每位嘉宾发出邀请。
# 3-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
# 以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
# 使用pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进
# 晚餐。
# 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
# # # 使用del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
# # len
# #
# #练习3-4
# meal_with = ['phyllis','ergou','erzi','momoe']
# n = 0
# x = len(meal_with)
# while n < x:#列表元素个数代码 #问题 n <= x 时报错 回头看
# invitation = meal_with[n].title() + ",Please have a meal with us."
# print(invitation)
# n += 1
# popped_meal_with =meal_with.pop(0)#问题 元素的位置有误
# regret = meal_with.pop(0).title() + " can't comes."#.pop此处已执行第二次,下文订正
# print(meal_with)
# print(regret)
# revisal = "以下为订正后结果"
# FBI_warning = revisal.center(30,'*')
# print(FBI_warning)
# meal_with2 = ['phyllis','ergou','erzi','momoe']
# print(meal_with2)
# popped_meal_with2 =meal_with2.pop(3)#问题 元素的位置有误
# regret2 = popped_meal_with2.title() + " can't comes.\n" + "*" * 38
# print(meal_with2)
# print(regret2)
#
# print("\nNow invite other girls.")
# meal_with2.append('shiitakeimoto')
# print(meal_with2)
# print('Now send invitations\n')
# n2 = 0
# x2 = len(meal_with2)
# while n2 < x2:#列表元素个数代码
# invitation2 = meal_with2[n2].title() + ",Please have a meal with us."
# print(invitation2)
# n2 += 1

#3-6 more girls
meal_with2 = ['shiitakeimoto', 'ergou', 'erzi',"dudu"]
new_message = "Now I found one bigger table.Let me invite more girls!\n" + '*' * 54 + "\n"
print(new_message)
meal_with2.insert(0,'phyllis')
meal_with2.insert(-1,"soso")
number_of_people = len(meal_with2)
z = number_of_people/2 #结果如何输出为int
meal_with2.insert(3,"harashima")#问题 如何取列表元素个数的中位数
n3 = 0
x3 = number_of_people
while n3 <= x3: #问题 为何此时用 <= 不报错
invitation3 = meal_with2[n3].title() + " Please have a meal with us."
print(invitation3)
n3 += 1

# ['dudu', 'ergou', 'harashima', 'erzi', 'soso','phyllis', 'shiitakeimoto']
new_message2 = '\n\nUnfortunatlly.I odered table can\'t arrive today.So I only can invite 2 persons.I\'m so sorry to you\n'
print(new_message2)
print(meal_with2)
cancel = len(meal_with2)
while cancel > 3:
cancel = len(meal_with2)
popped_meal = meal_with2.pop()
sorry = popped_meal.title() + ",I'm so sorry."
print(sorry)
go_with1 = '\n' + meal_with2[0].title() + ',Please have a meal with me.'
go_with2 = meal_with2[1].title() + ',Please have a meal with me.'
print(go_with1)
print(go_with2)
# popped_they = they.pop()
# print(they)
# print(popped_they)
del meal_with2[0]
del meal_with2[0]
print(meal_with2)

猜你喜欢

转载自www.cnblogs.com/phyllissRyo/p/10389231.html