第四章序列的应用
实例01:输出每日一贴
1 import datetime 2 #定义一个列表 3 mot = ["今天星期一:\n坚持下去不是因为我很坚强,而是因为我别无选择。", 4 "今天星期二:\n含泪播种的人一定会有收货", 5 "今天星期三:\n做对的事情比把事情做对重要", 6 "今天星期四:\n命运给予我们的不是失望之酒,而是机会之杯", 7 "今天星期五:\n不要等到明天,明天太遥远,今天就行动", 8 "今天星期六:\n求知若渴,虚心若愚", 9 "今天星期七:\n成功将属于那些从不说“不可能”的人"] 10 day=datetime.datetime.now().weekday() 11 print(mot[day])
实例02:分开显示2017-2018赛季NBA西部联盟前八名的球队
1 print("2017-2018赛季NBA西部联盟前八名\n") 2 team = ["火箭","勇士","开拓者","雷霆","爵士","鹈鹕","马刺","森林狼"] 3 for index,item in enumerate(team): 4 if index%2 == 0: 5 print(item+"\t\t",end='') 6 else: 7 print(item+"\n")
实例03:向NBA名人堂列表中追加2018年新进入的球星
1 #NBA名人堂原有人员 2 oldlist = ["迈克尔·乔丹","卡里姆·阿布杜尔·贾巴尔","哈吉姆·奥拉朱旺","查尔斯·巴克利","姚明"] 3 newlist = ["贾森·基德","史蒂夫·纳什","格兰特·希尔"] 4 oldlist.extend(newlist) 5 print(oldlist)
实例04:使用二维列表输出不同版本的古诗
1 str1 = "千山鸟飞绝" 2 str2 = "万径人踪灭" 3 str3 = "孤舟蓑笠翁" 4 str4 = "独钓寒江雪" 5 verse = [list(str1),list(str2),list(str3),list(str4)] 6 print("\n--横版--\n") 7 for i in range(4): 8 for j in range(5): 9 if j == 4: 10 print(verse[i][j]) 11 else: 12 print(verse[i][j],end="") 13 verse.reverse() 14 print("\n--竖版--\n") 15 for i in range(5): 16 for j in range(4): 17 if j == 3: 18 print(verse[j][i]) 19 else: 20 print(verse[j][i],end="")
实例05:使用元组保存咖啡馆里提供的咖啡名称
1 coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡','麝香猫','哥伦比亚') 2 print(coffeename)
实例06:使用for循环列出咖啡馆里的咖啡名称
1 coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡','麝香猫','哥伦比亚') 2 print("你好,欢迎光临~依米咖啡馆~\n\n我店有:\n") 3 for name in coffeename: 4 print(name+"咖啡",end = " ")
实例07:分两列显示2017-2018赛季NBA西部联盟前八名球队
1 print("2017-2018赛季NBA西部联盟前八名\n") 2 team = ["火箭","勇士","开拓者","雷霆","爵士","鹈鹕","马刺","森林狼"] 3 for index,item in enumerate(team): 4 if index%2 == 0: 5 print(item+ "\t\t",end='') 6 else: 7 print(item + "\n")
实例08:将麝香猫咖啡替换为拿铁咖啡
1 coffeename = ('蓝山','卡布奇诺','曼特宁','摩卡','麝香猫','哥伦比亚') 2 coffeename[4] = '拿铁' 3 print(coffeename)
实例09:创建一个保存女神星座的字典
1 name = ['绮梦','冷依一','香凝','黛兰'] 2 sign = ['水瓶座','射手座','双鱼座','双子座'] 3 dictionary = dict(zip(name,sign)) 4 print(dictionary)
实例10:根据星座测试性格特点
1 name = ['绮梦','冷依一','香凝','黛兰'] 2 sign_person = ['水瓶座','射手座','双鱼座','双子座'] 3 person_dict = dict(zip(name,sign_person)) 4 sign_all=['白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座','水瓶座','双鱼座'] 5 nature=['有一种让人看见就觉得开心的感觉,阳光、乐观、坚强,性格直来直去,就是有点小脾气', 6 '很保守,喜欢稳定,一旦有什么变动就会觉得心里不踏实,性格比较慢热,是个理财高手', 7 '喜欢追求新鲜感,有点小聪明,耐心不够,因你的可爱性格会让很多人喜欢和你做朋友', 8 '情绪容易敏感,缺乏安全感,做事情有坚持到底的毅力,为人重义,对朋友和家人特别忠实', 9 '有着远大的理想,总想靠自己的努力成为人上人,总是期待被仰慕被崇拜的感觉', 10 '坚持追求自己的完美主义者', 11 '追求平等、和谐,交际能力强,因此朋友较多。最大的缺点就是面对选择总是犹豫不决。', 12 '精力旺盛,占有欲强,对于生活很有目标,不达目的誓不哭休,复仇心重', 13 '崇尚自由,勇敢、果断、独立身上有一股勇往直前的劲儿,只要想做,就能做。', 14 '是最有耐心的,做事最小心。做事脚踏实地比较固执,不达目的不罢休,而且非常勤奋', 15 '人很聪明,最大的特点是创新,追求独一无二', 16 '集所有星座的优缺点子一身 的生活,个人主义色彩很浓重的星座。帮助别人。'] 17 sign_dict = dict(zip(sign_all,nature)) 18 print("【香凝】的星座是",person_dict.get("香凝")) 19 print("\n她的性格特点是:\n\n",sign_dict.get(person_dict.get("香凝")))
实例11:应用字典推导式实现根据名字和星座创建一个字典
1 name = ['绮梦','冷依一','香凝','黛兰'] 2 sign = ['水瓶','射手','双鱼','双子'] 3 dictionary = {i:j+'座'for i,j in zip(name,sign)} 4 print(dictionary)
实例12:创建保存学生选课信息的集合、
1 python = {'绮梦','冷依一','香凝','梓轩'} 2 c = {'冷依一','零语','梓轩','圣博'} 3 print('选择python语言的学生有:',python,'\n') 4 print('选择c语言的学生有:',c)
实例13:学生更改选学课程
1 python = set(['绮梦','冷依一','香凝','梓轩']) 2 python.add('零语') 3 c = set(['冷依一','零语','梓轩','圣博']) 4 c.remove('零语') 5 print('选择python语言的学生有:',python,'\n') 6 print('选择c语言的学生有:',c)
实例14:对选课集合进行交集、并集和差集运算
1 python = set(['绮梦','冷依一','香凝','梓轩']) 2 c = set(['冷依一','零语','梓轩','圣博']) 3 print('选择python语言的学生有:',python,'\n') 4 print('选择c语言的学生有:',c) 5 print('交集运算:',python&c) 6 print('并集运算:',python | c) 7 print('差值运算:',python -c)
实战1:输出:“王者荣耀”的游戏角色
1 print("“王者荣耀“游戏角色:") 2 print("====坦克:====") 3 tank = ["苏烈","刘邦","钟馗","张飞","牛魔","程咬金","白起","刘禅","庄周","项羽","廉颇","巨灵神","安禄山","猪八戒"] 4 for i in tank: 5 print(i,end=" ") 6 print("\n"+"====战士:====") 7 zhanshi = ["狂铁","裴擒虎","铠","孙悟空","哪吒","杨戬","橘右京","亚瑟","雅典娜","夏侯惇","关羽","吕布","韩信","老夫子","达摩", 8 "典韦","曹操","钟无艳","墨子","赵云","刑天","龙且"] 9 for i in zhanshi: 10 print(i,end=" ") 11 print("\n"+"====刺客:====") 12 cike = ["百里玄策","庞统","花木兰","靳柯","不知火舞","李白","娜可露露","兰陵王","韩信","宫本武藏","盖聂","红拂"] 13 for i in cike: 14 print(i,end=" ") 15 print("\n"+"====法师:====") 16 fashi = ["杨玉环","奕星","女娲","周瑜","鬼谷子","芈月","东皇太一","大乔","诸葛亮","貂蝉","张良","安琪拉"] 17 for i in fashi: 18 print(i,end=" ") 19 print("\n"+"====射手:====") 20 sheshou = ["公孙离","百里守约","后羿","刘备","黄忠","马可波罗","成吉思汗","虞姬","李元芳","艾琳","狄仁杰","鲁班七号","孙尚香"] 21 for i in sheshou: 22 print(i,end=" ") 23 print("\n"+"====辅助:====") 24 fuzhu = ["明世隐","梦奇","孙膑","太乙真人","蔡文姬"] 25 for i in fuzhu: 26 print(i,end=" ")
实战2:模拟火车订票系统
1 biaotou = ["车次","出发站-到达站","出发时间","到达时间","历时"] 2 checi = ["T40","T298","Z158","Z62"] 3 chufa_daoda = ["长春-北京","长春-北京","长春-北京","长春-北京"] 4 start_time = ["00:12","00:06","12:48","21:58"] 5 daoda_time = ["12:20","10:50","21:06","06:08"] 6 lishi = ["12:08","10:44","08:18","8:20"] 7 for i in biaotou: 8 print(i,end="") 9 print("\n") 10 print(checi[0],'',chufa_daoda[0],'',start_time[0],daoda_time[0],'',lishi[0],'') 11 print(checi[1],'',chufa_daoda[1],'',start_time[1],'',daoda_time[1],'',lishi[1],'') 12 print(checi[2],'',chufa_daoda[2],'',start_time[2],'',daoda_time[2],'',lishi[2],'') 13 print(checi[3],'',chufa_daoda[3],'',start_time[3],'',daoda_time[3],'',lishi[3],'') 14 chufa_chx = dict(zip(checi,chufa_daoda,)) 15 start_time_chx = dict(zip(checi,start_time)) 16 daoda_time_chx = dict(zip(checi,daoda_time)) 17 lishi_chx = dict(zip(checi,lishi)) 18 acheci = input("请输入购买车次") 19 goumairen = input("请输入乘车人(用逗号分隔):") 20 achufa = chufa_chx[acheci] 21 print("你已购买"+acheci+"次列车"+achufa+"开,请"+goumairen+"尽快换取纸质车票。【铁路客服】")
实战3:电视剧的收视率排行榜
1 print("电视剧的收视率排行榜") 2 TV_plays = [('《Give up,hold to me》',1.4), 3 ('《The private dishes of the husbands》',1.34), 4 ('《My father-in-law will do martiaiarts》',0.92), 5 ('《North Canton still believe in love》',0.862), 6 ('《Impossible task》',0.553), 7 ('《Sparrow》',0.411), 8 ('《East of dream Avenue》',0.164), 9 ('《The prodigal son of the new frontier town》',0.259), 10 ('《Distant distant》',0.394), 11 ('《Music legend》',0.562), 12 ] 13 TV_plays=sorted(TV_plays,key = lambda s:s[1],reverse=True) 14 for TV_play in TV_plays: 15 print(TV_play[0]+"收视率:"+str(TV_play[1])+"%")
实战4:定制自己的手机套餐
1 calling_plan = ["0分钟","50分钟","100分钟","300分钟","不限量"] 2 flow_plan = ["0M","500M","1G","5G","不限量"] 3 message_plan = ["0条","50条","100条"] 4 print("定制自己的手机套餐:") 5 #A.通话时长的选择 6 print("A.请设置通话时长:") 7 for index,calling in enumerate(calling_plan): 8 print(str(index+1)+"."+calling) 9 user_calling_plan = int(input("输入选择的通话时长编号:")) 10 while user_calling_plan > len(calling_plan) or user_calling_plan <= 0 : 11 user_calling_plan = int(input("请输入正确的套餐编号:")) 12 if 0 < user_calling_plan <= len(calling_plan): 13 break 14 numA = user_calling_plan 15 #B.流量的选择 16 print("B.请设置流量包:") 17 for index,flow in enumerate(flow_plan): 18 print(str(index+1)+"."+flow) 19 user_flow_plan = int(input("输入选择的流量包编号:")) 20 while user_flow_plan > len(flow_plan) or user_flow_plan <= 0 : 21 user_flow_plan = int(input("请输入正确的套餐编号:")) 22 if 0 < user_flow_plan <= len(flow_plan): 23 break 24 numB = user_flow_plan 25 #C.短信的选择 26 print("C.请设置短信条数:") 27 for index,message in enumerate(message_plan): 28 print(str(index+1)+"."+message) 29 user_message_plan = int(input("输入选择的短信条数编号:")) 30 while user_message_plan > len(message_plan) or user_message_plan <= 0 : 31 user_message_plan = int(input("请输入正确的套餐编号:")) 32 if 0 < user_message_plan <= len(message_plan): 33 break 34 numC = user_message_plan 35 #输出套餐结果 36 print("您的手机套餐定制成功:" 37 +"免费通话时长为"+calling_plan[numA-1]+"/月," 38 +"流量为"+flow_plan[numB-1]+"/月," 39 +"短信条数"+message_plan[numC-1]+"/月," 40 )
标签:end,python,daoda,print,例子,plan,应用,time,序列 From: https://www.cnblogs.com/psl-12/p/16741038.html