Python.<习题六> 字典与集合

11.编写程序,对用户输入的英文字符串中各字母出现的次数进行统计(不区分大写字母和小写字母),统计结果使用字典存放。例如,字符串"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)


12.已知字符串变量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])


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

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)


14.假设字典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])


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

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]))


16.某超市整理库存。假设字典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]))


17.

n=int(input("请设置加密位移数:"))
dic_convertor={}
for i in range(26):ming=ord("a")+i   #ming为键的ASCII编码ch=ming+nif ch<=122:mi=ch       else:mi=ch-122+96dic_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="")


18.

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_numberprint("-------------------\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":breakelse:print("输入错误!")print("-------------------\n")
print("-----程序已结束-----")


19.

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)))


20.

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_intersection))
print("只参加跳高比赛的有:{}".format(set_difference1))
print("只参加跳远比赛的有:{}".format(set_difference2))
print("只参加一项比赛的有:{}".format(set_symmetric_difference))

Python.习题六 字典与集合(下)相关推荐

  1. 刻意练习:Python基础 -- Task06. 字典与集合

    背景 我们准备利用17天时间,将 "Python基础的刻意练习" 分为如下任务: Task01:变量.运算符与数据类型(1day) Task02:条件与循环(1day) Task0 ...

  2. 【Python核心】字典和集合

    除了列表和元组,接下来看看两个同样很常见并且很有用的数据结构:字典(dict)和集合(set) 字典和集合在Python被广泛使用,并且性能进行了高度优化,其重要性不言而喻 一.字典和集合基础 1.1 ...

  3. Python进阶7——字典与集合

    1.创建字典的五种方式 d1=dict(one=1, two=2) d2={'one':1, 'two':2} d3=dict(zip(['one', 'two'], [1,2])) d4=dict( ...

  4. 微课|Python列表、字典、集合、字符串对象常用方法串讲

    推荐教材: <Python网络程序设计(微课版)>,ISBN:978-7-3025-8312-7,董付国,清华大学出版社,2021年8月出版 配套资源: 教学大纲.450页PPT.91个例 ...

  5. Python基础三--字典,集合,编码,深浅copy,元祖、文件操作

    字典 dict数据类型划分:可变数据类型,不可变数据类型不可变数据类型: 元组,bool值,int,str 可哈希可变数据类型: list,dict,set 不可哈希 dict key :必须是不可变 ...

  6. python - 元组,字典,集合

    元组(tuple) 元组·的三个特点: 1.有序项目集合. 2.可以存放任何类型的数据 3.元组是不可变数据类型 元组的定义: >>> tu1 = ()>>> ty ...

  7. python怎么建立字典翻译_Python下字典创建的8种方法

    Python下字典创建的几种方法记录 1.创建空字典 >>> info_dict = {} >>> print(type(info_dict)) 2.赋值直接生成 ...

  8. python中的字典和集合_Python 字典和集合

    字典的每个键值对用冒号分割,键值对之间用逗号分隔,所有键值对包括在{}中. d = {key1 : value1, key2 : value2 } 键必须是唯一的,值可以不唯一.值可以取任何数据类型, ...

  9. python教材答案字典与集合_Python——集合与字典练习

    集合与字典练习 question1 问题描述:有一个列表,其中包括 10 个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表 ...

  10. python列表,字典,集合

    初识模块 import sysprint(sys.path)#查看化境变量print(sys.argv)#查看文件的相对路径,但是在pachrm中 会自动转为绝对路径 View Code import ...

最新文章

  1. 使用VIA声卡 运行生化危机5无声音的解决方案 无需卸载旧驱动
  2. 嵌入式系统低功耗管理(备忘)
  3. Fanout交换器-编写生产者
  4. Android—Bitmap图片大小计算、压缩与三级缓存
  5. 湖南大学计算机学院张柏杨,缪力-湖大信息科学与工程学院
  6. 2017已过半,这半年,你累吗?
  7. 阿里数据库内核月报:2017年04月
  8. Hadoop背景、模块介绍、架构
  9. java中怎么删除多表连接_在Java中从多个列表中合并和删除重复的最佳方式
  10. Android APK反编译详解
  11. anaconda环境中使用sudo python报错
  12. 路径规划之空间采样算法研究现状简述
  13. W3School-CSS 字体(font)实例
  14. 控制直流电机正反转以及刹车电路设计
  15. SplitContainer的使用
  16. 微信聊天记录做成词云~
  17. Vue表情包输入组件
  18. google服务框架 闪退_没Google服务闪退?教你解决手游谷歌服务问题
  19. 微信小游戏实战--cocos creator实现wordle游戏(二)
  20. 商洛师范学院计算机老师,我校在首届全国师范生微课大赛中获佳绩

热门文章

  1. 一文入门智能开关的3种功能形态
  2. Unicode中的数学符号
  3. 百度 tts 语音合成前端无法播放问题解决
  4. iOS CMMotionManager之加速计、陀螺仪
  5. 数字PCR和实时PCR的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  6. 如何更好更快的站在巨人的肩膀上?
  7. 北大公开课计算机,清华、北大、浙大的计算机课程资源集都在这里了
  8. Spring事务(Transaction)
  9. 微信小程序识别图片并提取文字_微信小程序图片上传(文字识别)
  10. qt 表格中插入一行_Qt在表格中加入控件