Python:列表与字典2-Routine6

列表与元组的再次一决高下:列表与元组相比固然有很多优势,但是也有例外,如元组的存储比列表快,这是因为元组的不可变性,计算机在存储他们的时候会用一种比列表快的方式。还有元组的不可变性使它们成为创建变量的好办法,可以为代码加上一层安全保护。除此之外,元组在有些情况下非用不可,如,字典需要不可变类型,元组就成了创建某些字典的基础要求了。
嵌套序列(tested sequences):由于列表和元组都可以存放任何类型的数据,所以它们的元素都可以含有列表和元组,也就是序列中有序列。它是组织复杂信息的有力工具。
案例程序如下:

nestedseq = [('Mary', 80),('Jack',85),('Rose',90),('Peter',70),('Rose',99)]
print("""
                         Score List:

                  Choice       introduction
                  ↓↓↓          ↓↓↓

                    0 ------------ Exit.
                    1 ------------ Show the score list.
                    2 ------------ Add a score.
                    3 ------------ Change/Remove someone's score.
                    4 ------------ Rank the list.
                    5 ------------ Check someone's score
      """)
choice = 1
exchange = []
while choice:
           choice = (input("\nThe number of your choice:"))
           if choice == '0':
                      break 
           elif choice == '1':
                      print("Name\tScore")
                      for i in range(0,len(nestedseq)):
                                 print(nestedseq[i][0],"\t",nestedseq[i][1])
           elif choice == '2':
                      name = input("\nThe name you want to add is:")
                      print("The score of",name,"is:",end = " ")
                      score = int(input())
                      nestedseq.append((name,score))
           elif choice == '3':
                      yourwill = input("\nChange or Remove?:")
                      if yourwill.lower() == 'change':
                                 name = input("\nWhose score do you want to change?:")
                                 mark = 0
                                 for i in range(0,len(nestedseq)):
                                            if name.lower() == nestedseq[i][0].lower():
                                                       print("The score of'",name,"'will be changed to?:",end = " ")
                                                       score = int(input())
                                                       nestedseq[i] = (name,score)                                                       
                                                       mark += 1
                                 if mark:
                                            print("The number of the name'",name,"'you have changed is:",mark)
                                            print("Now the list is changed to be:")
                                            print("Name\tScore")
                                            for i in range(0,len(nestedseq)):
                                                       print(nestedseq[i][0],"\t",nestedseq[i][1])
                                 else:
                                            print("There is no such name.")                                            
                      elif yourwill.lower() == 'remove':
                                 name = input("\nThe name you want to remove is:")
                                 score = int(input("\nAnd the score is:"))
                                 mark = 0
                                 if (name,score) in nestedseq:#name要符合大小写规范
                                            nestedseq.remove((name,score))
                                            mark = 1

                                 if mark:
                                            print("The number of the name'",name,"'you have removed is:",mark)
                                            print("Now the list is:")
                                            print("Name\tScore")
                                            for i in range(0,len(nestedseq)):
                                                       print(nestedseq[i][0],"\t",nestedseq[i][1])
                                 else:
                                            print("There is no such item.")

                      else:
                                 print("I can't recognize your will.")
           elif choice == '4':
                      for j in range(0,len(nestedseq)):
                                 for i in range(0,len(nestedseq)):
                                            if (i+1) == len(nestedseq):
                                                       break
                                            elif nestedseq[i][1] >= nestedseq[i+1][1]:
                                                       continue
                                            else:
                                                       middlepoint = nestedseq[i]
                                                       nestedseq[i] = nestedseq[i+1]
                                                       nestedseq[i+1] = middlepoint
                      print("Now the list is:")
                      print("Name\tScore")
                      for i in range(0,len(nestedseq)):
                                 print(nestedseq[i][0],"\t",nestedseq[i][1])
                      rev = input("Do you want to reverse it?Yes or No:")
                      if rev.lower() == 'yes':
                                 nestedseq.reverse()
                                 print("After reversing it:")
                                 print("Name\tScore")
                                 for i in range(0,len(nestedseq)):
                                            print(nestedseq[i][0],"\t",nestedseq[i][1])
                      else:
                                 print()
           elif choice == '5':
                      name = input("\nWhat's the name that you want to check?:")
                      mark1 = 0
                      for i in range(0,len(nestedseq)):
                                 if name.lower() == nestedseq[i][0].lower():
                                            exchange.append((name,nestedseq[i][1]))
                                            mark1 += 1
                      if mark1:
                                 print("\nThe number of the name you are checking is:",mark1)
                                 print("Name\tScore")
                                 for i in range(0,len(exchange)):
                                            print(exchange[i][0],"\t",exchange[i][1])
                      else:
                                 print("\nThe name you give is invalid!")
           else:
                      print("\nYour choice is illegal.")      
input("\nPress the enter key to exit!")

序列的解包:

           nestedseq = [('Mary', 80),('Jack',85),('Rose',90),('Peter',70),('Rose',99)]
           name,score = nestedseq[2]
           print("Name\tScore")
           print(name,"\t",score)

这就叫解包(upacking),它对任何序列都有效,但是左边变量的数量要和右边序列中的元素的数量相等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值