例题3-10 尝试使用各个函数
描述: 建立一个列表,对本章的每个函数,至少使用一次来处理。
代码:
#coding=utf-8
attractions = ['Spain', 'Turkey', 'Morocco', 'Japan', 'Finland']
attractions.append('France') #使用append()添加到列表最后
attractions.append('Singapore')
print(attractions)
attractions.insert(-1,'Iceland') #使用insert()添加到列表最后,实际是替换了原列表最后元素
print(attractions) #看出原来最后的元素依然是最后一个
del attractions[-1] #使用del删除最后一个元素
print(attractions)
attractions.pop() #使用pop()删除最后一个元素
print(attractions)
attractions.remove('France') #使用remove()删除给定值
print(attractions)
print(sorted(attractions, reverse = True)) #使用sorted按非字母顺序排序,不改变原值
print(attractions)
attractions.reverse() #使用reverse反向排序
print(attractions)
attractions.sort(reverse = True) #使用sort按非字母顺序排序,改变原值
print(attractions)
print(len(attractions)) #打印列表长度
结果:
例题3-8 放眼世界
描述: 对列表使用sorted(),sort()和reverse()方法,比较它们。
代码:
#coding=utf-8
attractions = ['Spain', 'Turkey', 'Morocco', 'Japan', 'Finland']
print(attractions)
print(sorted(attractions)) #字母顺序,不改变
print(attractions)
print(sorted(attractions, reverse = True)) #非字母顺序
print(attractions)
attractions.reverse() #反序,改变
print(attractions)
attractions.reverse()
print(attractions)
attractions.sort() #顺序,改变
print(attractions)
attractions.sort(reverse=True) #非字母顺序
print(attractions)
结果:
例题3-4to3-7 嘉宾名单
描述: 建立嘉宾名单,进行修改,添加,删除等操作
代码:
#coding=utf-8
guests = ['grandpa', 'grandma', 'sister']
message1 = guests[0].title() + ", would you like to join my dinner party?"
message2 = guests[1].title() + ", would you like to join my dinner party?"
message3 = guests[2].title() + ", would you like to join my dinner party?"
print(message1)
print(message2)
print(message3)
message4 = '\n' + guests[-1].title() + " cannot join the party."
print(message4)
guests[-1] = 'father' #修改嘉宾信息
message1 = guests[0].title() + ", would you like to join my dinner party?"
message2 = guests[1].title() + ", would you like to join my dinner party?"
message3 = guests[2].title() + ", would you like to join my dinner party?"
print(message1)
print(message2)
print(message3)
message4 = '\nWhen I have found a bigger table...'
print(message4)
guests.insert(0, 'mother') #添加嘉宾到第一位
guests.insert(2, 'aunt') #添加到中间
guests.append('brother') #添加到末尾
message1 = guests[0].title() + ", would you like to join my dinner party?"
message2 = guests[1].title() + ", would you like to join my dinner party?"
message3 = guests[2].title() + ", would you like to join my dinner party?"
message4 = guests[3].title() + ", would you like to join my dinner party?"
message5 = guests[4].title() + ", would you like to join my dinner party?"
message6 = guests[5].title() + ", would you like to join my dinner party?"
print(message1)
print(message2)
print(message3)
print(message4)
print(message5)
print(message6)
message = 'I have invited ' + str(len(guests)) + ' guests to my dinner party.'
print(message)
message5 = '\nOne sad news came that my table can only serve two guests...'
print(message5)
#删除嘉宾至只剩两人
while len(guests)>2 :
guest = guests.pop()
message = guest.title() + ', I am so sorry that I cannot invite you to my dinner party.'
print(message)
message = guests[0].title() + ', you are still invited.'
print(message)
message = guests[1].title() + ', you are still invited.'
print(message)
del guests[0] #删除所有嘉宾
del guests[0]
print(guests) #检查名单是否为空
结果:
本次练习遇到的问题:
- 第一次用到while语句,要记住限定条件,冒号,缩进。
- sorted非字母顺序的用法:sorted(list,reverse=True)。
- 删除列表最后两个元素时,先删除了0,再删除时仍用1,导致错误。
- len返回值为int,记得使用str()在必要时。
- insert在-1位置时,是替换原来最后一个元素的位置,原元素后移。