python 实验七 字典与集合 (下) 您所在的位置:网站首页 24字母表的顺序幼儿园 python 实验七 字典与集合 (下)

python 实验七 字典与集合 (下)

2024-03-01 12:35| 来源: 网络整理| 查看: 265

12.某幼儿园组织秋游,需要统计人数。假设字典dic_class存放了幼儿园所有的班级,内容为{“托班”:[“聪聪班”,“伶伶班”,“楠楠班”],“小班”:[“小一班”,“小二班”],“中班”:[“中一班”,“中二班”],“大班”:[“大一班”,“大二班”]}。字典dic_number存放了每个班的报名人数,内容为{“聪聪班”:26,“伶伶班”:23,“楠楠班”:25,“小一班”:32,“小二班”:31,“中一班”:33,“中二班”:34,“大一班”:32,“大二班”:33}。试编写程序,统计各年级报名人数以及全员报名总人数。

dic_托班":["聪聪班","伶伶班","楠楠班"],"小班":["小一班","小二班"],"中班":["中一班","中二班"],"大班":["大一班","大二班"]} dic_number={"聪聪班":26,"伶伶班":23,"楠楠班":25,"小一班":32,"小二班":31,"中一班": 33,"中二班":34,"大一班":32,"大二班":33} dic_totle={} for k,v in dic_class.items(): s=0 for x in v: s+=dic_number[x] dic_totle[k]=s for k,v in dic_totle.items(): print("{}:{}人".format(k,v)) print("全园:{}人".format(sum(dic_totle.values())))

#输出结果

托班:74人 小班:63人 中班:67人 大班:65人 全园:269人

13.编写程序,对用户输入的英文字符串中各字母出现的次数进行统计(不区分大写字母和小写字母),统计结果使用字典存放。例如,字符串"I have 2 ideas."的统计结果为{“i”:2,“h”:1,“a”:2,“v”:1,“e”:2,“d”:1,“s”:1}。假设用户输入的字符串中可能包含字母以外的其他字符。

s=input("请输入字符串:") myDict={} for c in s: ch=c.lower() if ch.isalpha(): myDict [ch]= myDict.get(ch,0)+1 print(myDict)

#输出样例

请输入字符串:I have 2 ideas {‘i’: 2, ‘h’: 1, ‘a’: 2, ‘v’: 1, ‘e’: 2, ‘d’: 1, ‘s’: 1}

14.已知字符串变量s=“When in the course of human events,it becomes necessary for one people to dissolve the political bands which have connected them with another,and to assume among the powers of the earth,the separate and equal atation to which the Laws of Nature and of Nature’s God entitle them,a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.”,存放了美国独立宣言中的一段话。试编写程序,实现以下功能: (1)对文本中每个单词出现的次数进行统计,并将结果输出。 (2)输出出现次数排在前五名的单词。

s='''When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the Powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.''' s=s.lower().replace(',','').replace('.','') lst=s.split(' ') dic={} for word in lst: dic[word]=dic.get(word,0)+1 print(dic) newlst=[(v,k) for k,v in dic.items()] newlst.sort() print(newlst[-1:-6:-1])

#输出结果

{‘when’: 1, ‘in’: 1, ‘the’: 9, ‘course’: 1, ‘of’: 5, ‘human’: 1, ‘events’: 1, ‘it’: 1, ‘becomes’: 1, ‘necessary’: 1, ‘for’: 1, ‘one’: 1, ‘people’: 1, ‘to’: 5, ‘dissolve’: 1, ‘political’: 1, ‘bands’: 1, ‘which’: 3, ‘have’: 1, ‘connected’: 1, ‘them’: 3, ‘with’: 1, ‘another’: 1, ‘and’: 3, ‘assume’: 1, ‘among’: 1, ‘powers’: 1, ‘earth’: 1, ‘separate’: 1, ‘equal’: 1, ‘station’: 1, ‘laws’: 1, ‘nature’: 1, “nature’s”: 1, ‘god’: 1, ‘entitle’: 1, ‘a’: 1, ‘decent’: 1, ‘respect’: 1, ‘opinions’: 1, ‘mankind’: 1, ‘requires’: 1, ‘that’: 1, ‘they’: 1, ‘should’: 1, ‘declare’: 1, ‘causes’: 1, ‘impel’: 1, ‘separation’: 1} [(9, ‘the’), (5, ‘to’), (5, ‘of’), (3, ‘which’), (3, ‘them’)]

15.编写程序,使用嵌套字典描述表中内容之间的映射关系,输出字典中每种颜色的事物数目,如紫色的食物有三个。

表7-6 蔬菜 水果 饮料 青菜 绿色 山竹 紫色 椰子汁 白色 胡萝卜 橙色 香蕉 黄色 西瓜汁 红色 茄子 紫色 橘子 橙色 玉米汁 黄色 毛豆 绿色 草莓 红色 葡萄汁 紫色 dic_menu={"蔬菜":{"青菜":"绿色","胡萝卜":"橙色","茄子":"紫色","毛豆":"绿色"}, "水果":{"山竹":"紫色","香蕉":"黄色","橙子":"橙色","草莓":"红色"}, "饮料":{"椰子汁":"白色","西瓜汁":"红色","玉米汁":"黄色","葡萄汁":"紫色"}} dic_color={} for k,v in dic_menu.items(): for key,value in v.items(): dic_color[value]=dic_color.get(value,0)+1 print(dic_color)

#输出结果

{‘绿色’: 2, ‘橙色’: 2, ‘紫色’: 3, ‘黄色’: 2, ‘红色’: 2, ‘白色’: 1}

16.假设字典dic_score存放了学生的成绩,内容为{“李刚”:93,“李静”:78,“张金柱”:88,“赵启山”:91,“李鑫”:65,“黄宁”:83}。试编写程序,按照成绩从高到低的顺序输出学生的姓名。

dic_score={"李刚":93,"陈静":78,"张金柱":88,"赵启山":91,"李鑫":65,"黄宁":83} lst_name=dic_score.keys() lst_score=dic_score.values() dic=zip(lst_score,lst_name) for x in sorted(dic,reverse=True): print(x[1])

#输出结果

李刚 赵启山 张金柱 黄宁 陈静 李鑫

17.假设字典dic_house存放了某小区在售二手房的房源信息。试编写程序,实现以下功能: (1)找出挂牌价最低的三套房源,并输出相应的房源信息。 (2)找出人气最高的三套房源,并输出相应的房源信息。

表7-7 房源编号 房型 面积/平方米 朝向 装修情况 挂牌价/(元/平方米) 关注人数 001 3室1厅 68.69 南北 简装 37124 35 002 2室2厅 87.16 南西 精装 37375 148 003 3室1厅 61.72 南北 精装 37266 146 004 3室2厅 68.18 南北 精装 68496 79 005 2室2厅 71.67 南 简装 33487 105 006 3室1厅 84.78 南北 简装 51782 34 dic_house={"001":["3室1厅","68.69平方米","南北","简装","37124元/平方米","35人"],"002":["2室2厅","87.16平方米","南西","精装","37375元/平方米","148人"],"003":["3室1厅","61.72平方米","南北","精装","37266元/平方米","146人"],"004":["2室2厅","68.18平方米","南北","精装","68496元/平方米","79人"],"005":["2室2厅","71.67平方米","南","简装","33487元/平方米","105人"],"006":["3室1厅","84.78平方米","南北","简装","51782元/平方米","34人"]} lst_result1=sorted(dic_house.items(),key=lambda x:int(x[1][4][:-5])) print("单价最低的三套房源:") for i in range(3): print("房源编号:{},房源信息:{}".format(lst_result1[i][0],lst_result1[i][1])) lst_result2=sorted(dic_house.items(),key=lambda x:int(x[1][5][:-1]),reverse=True) print("人气最高的三套房源:") for i in range(3): print("房源编号:{},房源信息:{}".format(lst_result2[i][0],lst_result2[i][1]))

#输出结果

单价最低的三套房源: 房源编号:005,房源信息:['2室2厅', '71.67平方米', '南', '简装', '33487元/平方米', '105人'] 房源编号:001,房源信息:['3室1厅', '68.69平方米', '南北', '简装', '37124元/平方米', '35人'] 房源编号:003,房源信息:['3室1厅', '61.72平方米', '南北', '精装', '37266元/平方米', '146人'] 人气最高的三套房源: 房源编号:002,房源信息:['2室2厅', '87.16平方米', '南西', '精装', '37375元/平方米', '148人'] 房源编号:003,房源信息:['3室1厅', '61.72平方米', '南北', '精装', '37266元/平方米', '146人'] 房源编号:005,房源信息:['2室2厅', '71.67平方米', '南', '简装', '33487元/平方米', '105人']

18.某超市整理库存。假设字典dic_repertory=[“酱油”:50,“醋”:60,“盐”:100,“糖”:120,“鸡精”:20,“麻油”:40},存储了超市最初的商品数量。字典dic_change={“酱油”:100,“醋”:80,“鸡精”:50,“耗油”:60},存储了经过销售和进货等流程后发生变化的商品及其现有数量。试编写程序,实现以下功能: (1)对字典dic_repertory的内容进行更新。 (2)对更新后的字典dic_repertory按照商品数量进行降序排列。 (3)输出当前库存数量最多的商品和最少的商品信息。

dic_repertory={"酱油":50,"醋":60,"盐":100,"糖":120,"鸡精":20,"麻油":40} dic_change={"酱油":100,"醋":80,"鸡精":50,"蚝油":60} dic_repertory.update(dic_change) dic_result=sorted(zip(dic_repertory.values(),dic_repertory.keys()),reverse=True) print("库存最多的商品是:{}".format(dic_result[0][1])) print("库存最少的商品是:{}".format(dic_result[-1][1]))

#输出结果

库存最多的商品是:糖 库存最少的商品是:麻油

19.凯撒密码是密码学中一种简单且广为人知的加密技术,其本质是将明文中的所有数字按照字母表的顺序向后偏移固定数目后变成密文。例如,当偏移数目为3时,字母a映射成d,字母p映射成是,字母x映射成a,…试编写程序,实现以下功能: (1)提醒用户输入偏移数目,自动生成字母映射字典。 (2)提醒用户输入明文,根据字典映射关系对明文进行加密,并将密文输出。

n=int(input("请设置加密位移数:")) dic_convertor={} for i in range(26): ming=ord("a")+i #ming为键的ASCII编码 ch=ming+n if ch


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有