当前位置:   article > 正文

Python编程 从入门到实践 第六章习题_请参考例4-9,用嵌套的列表存储运动员

请参考例4-9,用嵌套的列表存储运动员

6-2 喜欢的数字:

  1. favorite_number = {
  2. 'alex' : 29,
  3. 'leo' : 23,
  4. 'mike' : 45,
  5. 'John' : 100,
  6. 'Phil' : 34,
  7. }
'
运行

输出:

  1. Alex's favorite number is 29.
  2. Leo's favorite number is 23.
  3. Mike's favorite number is 45.
  4. John's favorite number is 100.
  5. Phil's favorite number is 34.

6-5 河流

  1. rivers = {
  2. 'China': 'Changjiang',
  3. 'egypt': 'nile',
  4. 'brazil' : 'amazon'
  5. }
  6. print("Messages:")
  7. for river in rivers:
  8. print("The " + river.title() + " runs through " + rivers[river].title() + '.')
  9. print("\nThe following rivers have been mentioned:")
  10. for river in rivers.keys():
  11. print(river.title())
  12. print("\nThe following countries have been mentioned: ")
  13. for country in rivers.values():
  14. print(country.title())
'
运行

输出:

  1. Messages:
  2. The China runs through Changjiang.
  3. The Egypt runs through Nile.
  4. The Brazil runs through Amazon.
  5. The following rivers have been mentioned:
  6. China
  7. Egypt
  8. Brazil
  9. The following countries have been mentioned:
  10. Changjiang
  11. Nile
  12. Amazon
6-6 调查
  1. favorite_language = {
  2. 'jen': 'python',
  3. 'sarah': 'c',
  4. 'edward': 'ruby',
  5. 'phil': 'python',
  6. }
  7. visited_friends = ['jen', 'sarah']
  8. for name in favorite_language.keys():
  9. if name in visited_friends:
  10. print(name.title() + ",you participated in the survey,thank you!")
  11. else:
  12. print(name.title() + ",would you like to join us?")
'
运行

输出:

  1. Jen,you participated in the survey,thank you!
  2. Sarah,you participated in the survey,thank you!
  3. Edward,would you like to join us?
  4. Phil,would you like to join us?

6-8 宠物:

  1. pets = {
  2. 'lili':{
  3. 'type' : 'cat',
  4. 'people' : 'alex',
  5. },
  6. 'jordon':{
  7. 'type': 'dog',
  8. 'people': 'mike',
  9. },
  10. 'jucy':{
  11. 'type': 'bird',
  12. 'people': 'leo',
  13. }
  14. }
  15. for name, infos in pets.items():
  16. print("\nName: " + name.title())
  17. print("\tType: " + infos['type'].title())
  18. print("\tHost: " + infos['people'].title())
'
运行

输出:

  1. Name: Lili
  2. Type: Cat
  3. Host: Alex
  4. Name: Jordon
  5. Type: Dog
  6. Host: Mike
  7. Name: Jucy
  8. Type: Bird
  9. Host: Leo

6-10 喜欢的数字:

  1. favorite_number = {
  2. 'jen': [23,45,34],
  3. 'sarah': [45,78],
  4. 'phil':[35,100,556,789],
  5. }
  6. for name, numbers in favorite_number.items():
  7. print("\n" + name.title() + "'s favorite numbers are: ")
  8. for number in numbers:
  9. print("\t" + str(number))
'
运行
输出:
  1. Jen's favorite numbers are:
  2. 23
  3. 45
  4. 34
  5. Sarah's favorite numbers are:
  6. 45
  7. 78
  8. Phil's favorite numbers are:
  9. 35
  10. 100
  11. 556
  12. 789

6-12 扩展

删去键和值:

  1. favorite_number = {
  2. 'jen': [23,45,34],
  3. 'sarah': [45,78],
  4. 'phil':[35,100,556,789],
  5. }
  6. del favorite_number['jen']
  7. for name, numbers in favorite_number.items():
  8. print("\n" + name.title() + "'s favorite numbers are: ")
  9. for number in numbers:
  10. print("\t" + str(number))
'
运行

输出:

  1. Sarah's favorite numbers are:
  2. 45
  3. 78
  4. Phil's favorite numbers are:
  5. 35
  6. 100
  7. 556
  8. 789

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号