当前位置:   article > 正文

《Python编程:从入门到实践》习题答案——第6章 字典

《Python编程:从入门到实践》习题答案——第6章 字典
  1. # 6-1
  2. ElonMusk= {'first_name':'Elon', 'last_name':'Musk', 'age':'53', 'city':'Pretoria'}
  3. for key,value in ElonMusk.items():
  4. print(key, ':', value)
  5. # 6-2 喜欢的数字
  6. favorite_number = {
  7. 'Tesla':'4',
  8. 'BMW':'2',
  9. 'NIO':'1',
  10. 'Huawei':'6',
  11. 'BYD':'8',
  12. }
  13. for k,v in favorite_number.items():
  14. print(k + "'s favorite number is " + v)
  15. # 6-3 词汇表
  16. dics = {
  17. 'int': '整形',
  18. 'bool': '布尔类型',
  19. 'float': '浮点型',
  20. 'str': '字符串',
  21. 'list': '列表',
  22. }
  23. print("int" + ":" + dics["int"])
  24. print("bool" + ":" + dics["bool"])
  25. print("float" + ":" + dics["float"])
  26. print("str" + ":" + dics["str"])
  27. print("list" + ":" + dics["list"])
  28. # 6-4 词汇表2
  29. dics = {
  30. 'int': '整形',
  31. 'bool': '布尔类型',
  32. 'float': '浮点型',
  33. 'str': '字符串',
  34. 'list': '列表',
  35. }
  36. for k,v in dics.items():
  37. print(k + ':' + v)
  38. # 6-5 河流
  39. rivers = {
  40. "ChangJiang":"China",
  41. "Nile":"Egypt",
  42. "Amazon":"Brazil",
  43. }
  44. for k,v in rivers.items():
  45. print(f"The {k} runs through {v}")
  46. for k in rivers.keys():
  47. print(k)
  48. for v in rivers.values():
  49. print(v)
  50. # 6-6 调查
  51. favorite_languages = {
  52. 'jen':'python',
  53. 'sarah':'c',
  54. 'edward':'ruby',
  55. 'phil':'python',
  56. }
  57. names = ['Musk', 'sarah', 'phil', 'LiMing']
  58. lists = []
  59. for k in favorite_languages.keys():
  60. lists.append(k)
  61. for name in names:
  62. if name in lists:
  63. print(f"{name}:Thank You!")
  64. else:
  65. print(f"{name}:Invite!")
  66. # 6-7
  67. ElonMusk= {'first_name':'Elon', 'last_name':'Musk', 'age':'53', 'city':'Pretoria'}
  68. MaYun= {'first_name':'Yun', 'last_name':'Ma', 'age':'60', 'city':'HangZhou'}
  69. WangChuanfu= {'first_name':'Chuanfu', 'last_name':'Wang', 'age':'58', 'city':'ShenZhen'}
  70. peoples = [ElonMusk, MaYun, WangChuanfu]
  71. for people in peoples:
  72. print(people)
  73. # 6-8 宠物
  74. feifei = {'type':'dog', 'master':'alien'}
  75. little_cute = {'type':'cat', 'master':'alice'}
  76. huahua = {'type':'panda', 'master':'tan'}
  77. pets = [feifei, little_cute, huahua]
  78. for pet in pets:
  79. print(pet)
  80. # 6-9 喜欢的地方
  81. favorite_places = {
  82. 'feifei':['Beijing', 'SuZhou'],
  83. 'Bob':['ShangHai','HangZhou', 'ShenZhen'],
  84. 'huahua':['Chengdu', 'ChongQing'],
  85. }
  86. for fp_k,fp_v in favorite_places.items():
  87. print(f"\n{fp_k}'s favorite places are:" )
  88. for v in fp_v: # 注意下
  89. print(v)
  90. # 6-10 喜欢的数字
  91. favorite_number = {
  92. 'Tesla':['4','5','6'],
  93. 'BMW':['5','6','7'],
  94. 'NIO':['1','2','3'],
  95. 'Huawei':['2','3','4'],
  96. 'BYD':['3','4','5'],
  97. }
  98. for fn_k,fn_v in favorite_number.items():
  99. print(f"\n{fn_k}'s favorite numbers are:")
  100. for v in fn_v:
  101. print(v)
  102. # 6-11 喜欢的数字
  103. citys = {
  104. 'ChengDu':{
  105. 'country': 'China',
  106. 'population': '1200 0000',
  107. 'fact': 'panda'
  108. },
  109. 'ShangHai':{
  110. 'country': 'China',
  111. 'population': '2300 0000',
  112. 'fact': 'The Bund'
  113. },
  114. 'LosAngeles': {
  115. 'country': 'America',
  116. 'population': '400 0000',
  117. 'fact': 'Hollywood'
  118. }
  119. } # 字典中嵌套了三个字典
  120. for k,v in citys.items():
  121. print(f"\n{k}:")
  122. for k1,v1 in v.items():
  123. print(f"\t{k1}:{v1}")
  124. # 6-12 拓展

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/1007565
推荐阅读
相关标签
  

闽ICP备14008679号