文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
第1章
练习1
#!/usr/bin/env python3 farm_animals = ['cow','pig','horse'] domestic_animals = ['dog','cat','gold fish'] zoo_animals = ['lion','elephant','gorilla'] animals = farm_animals + domestic_animals + zoo_animals for index_value in range(len(animals)): print("{0:d}: {1!s}".format(index_value, animals[index_value]))
练习2
#!/usr/bin/env python3 animals_dictionary = {} animals_list = ['cow','pig','horse'] other_list = [4567,[4,'turn',7,'left'],'Animals are great.'] for index_value in range(len(animals_list)): if animals_list[index_value] not in animals_dictionary: animals_dictionary[animals_list[index_value]] = other_list[index_value] for key, value in animals_dictionary.items(): print("{0!s}: {1}".format(key, value))
练习3
#!/usr/bin/env python3 list_of_lists = [['cow','pig','horse'], ['dog','cat','gold fish'],\ ['lion','elephant','gorilla']] for animal_list in list_of_lists: max_index = len(animal_list) output = '' for index in range(len(animal_list)): if index < (max_index-1): output += str(animal_list[index])+',' else: output += str(animal_list[index])+'\n' print(output)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论