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

dic_class={"托班":["聪聪班","伶伶班","楠楠班"],"小班":["小一班","小二班"],"中班":["中一班","中二班"],"大班":["大一班","大二班"]}
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<=122:
        mi=ch       
    else:
        mi=ch-122+96
    dic_convertor[chr(ming)]=chr(mi)
for k,v in dic_convertor.items():
    print("{}:{}".format(k,v))
mingwen=input("请输入明文:")
for i in mingwen:
    print(dic_convertor[i],end="")

#输出样例

请设置加密位移数:3
a:d
b:e
c:f
d:g
e:h
f:i
g:j
h:k
i:l
j:m
k:n
l:o
m:p
n:q
o:r
p:s
q:t
r:u
s:v
t:w
u:x
v:y
w:z
x:a
y:b
z:c
请输入明文:mi
pl

20.编写程序,完成以下功能。
(1)设计一个空字典,用于存放用户的通讯录(包括姓名和电话号码)。
(2)程序运行后,输入以下提示信息:
a.新增联系人
b.查询联系人
c.删除联系人
d.退出程序
(3)根据用户选择,进入下一步。

dic_address={}
while True:
    print("-----通讯录管理-----")
    print("a、新增联系人\nb、查询联系人\nc、删除联系人\nd、退出程序")
    sel=input("请输入您的选择:")
    if sel=="a":
        new_name=input("请输入联系人姓名:")
        new_number=input("请输入联系人电话:")
        dic_address[new_name]=new_number
        print("-------------------\n")
    elif sel=="b":
        name=input("请输入联系人姓名:")
        if name in dic_address:
            print("该联系人的电话号码为:{}".format(dic_address[name]))
        else:
            print("该联系人不存在!")
        print("-------------------\n")
    elif sel=="c":
        name=input("请输入联系人姓名:")
        if name in dic_address:
            del dic_address[name]
            print("该联系人已从通讯录中删除!")
        else:
            print("该联系人不存在!")
        print("-------------------\n")
    elif sel=="d":
        break
    else:
        print("输入错误!")
        print("-------------------\n")
print("-----程序已结束-----")

#输出样例

-----通讯录管理-----
a、新增联系人
b、查询联系人
c、删除联系人
d、退出程序
请输入您的选择:a
请输入联系人姓名:ll
请输入联系人电话:16564646

21题
在这里插入图片描述

s= '''Whether the weather be fine, or whether the weather be not. Whether the weather be cold, or whether the weather be hot. We will weather the weather whether we like it or not.'''
s=s.lower().replace(',','').replace('.','')
lst=s.split(' ')
wordSet=set(lst)
print(wordSet)
print("一共出现了{}个单词。".format(len(wordSet)))

#输出结果

{‘the’, ‘not’, ‘weather’, ‘it’, ‘will’, ‘like’, ‘or’, ‘hot’, ‘whether’, ‘be’, ‘cold’, ‘we’, ‘fine’}
一共出现了13个单词。

22题
在这里插入图片描述

set_highjump={"李朋","王宇","张锁","刘松山","白旭","李晓亮"}
set_longjump={"王宇","唐英","刘松山","白旭","刘小雨","宁成"}
set_intersection=set_highjump.intersection(set_longjump)
set_union=set_highjump.union(set_longjump)
set_difference1=set_highjump.difference(set_longjump)
set_difference2=set_longjump.difference(set_highjump)
set_symmetric_difference=set_longjump.symmetric_difference(set_highjump)
print("参加比赛的有:{}".format(set_union))
print("两项比赛都参加的有:{}".format(set_intersection))
print("只参加跳高比赛的有:{}".format(set_difference1))
print("只参加跳远比赛的有:{}".format(set_difference2))
print("只参加一项比赛的有:{}".format(set_symmetric_difference))

#输出结果

参加比赛的有:{‘李晓亮’, ‘李朋’, ‘刘松山’, ‘宁成’, ‘王宇’, ‘张锁’, ‘白旭’, ‘唐英’, ‘刘小雨’}
两项比赛都参加的有:{‘刘松山’, ‘白旭’, ‘王宇’}
只参加跳高比赛的有:{‘李晓亮’, ‘李朋’, ‘张锁’}
只参加跳远比赛的有:{‘宁成’, ‘唐英’, ‘刘小雨’}
只参加一项比赛的有:{‘李晓亮’, ‘李朋’, ‘宁成’, ‘张锁’, ‘唐英’, ‘刘小雨’}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐