组合数据类型练习,英文词频统计实例
1、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
1 work = list("1232122331231") 2 print(work) 3 4 work.append('3') 5 print(work[-1]) 6 7 work.pop() 8 print(work[-1]) 9 10 work[-1] = 2 11 print(work[-1]) 12 13 print(work.index('3')) 14 print(work.count('1')) 15 print(work.count('3'))
输出结果:
['1', '2', '3', '2', '1', '2', '2', '3', '3', '1', '2', '3', '1'] 3 1 2 2 3 4
2、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
score = {1:'44', 2:'66', 3:'78', 4:'67', 5:'88' } print(score) score[6] = '98' print(score) score[6] = '99' print(score) score.pop(6) print(score) for i in score: print("{:>2} : {:<2}".format(i,score.get(i)))
输出结果:
{1: '44', 2: '66', 3: '78', 4: '67', 5: '88'} {1: '44', 2: '66', 3: '78', 4: '67', 5: '88', 6: '98'} {1: '44', 2: '66', 3: '78', 4: '67', 5: '88', 6: '99'} {1: '44', 2: '66', 3: '78', 4: '67', 5: '88'} 1 : 44 2 : 66 3 : 78 4 : 67 5 : 88
3、列表,元组,字典,集合的遍历。总结列表,元组,字典,集合的联系与区别。
s=list('456132798') t=set('4561327489') c={'小明':'1','小红':'3','小华':'2'} x=(1, 2, 3, 4, 5 ) print('列表的遍历为:',end='') for i in s: print(i,end='') print() print('元祖的遍历为:',end='') for i in x: print(i,end='') print() print('字典key的遍历为:',end='') for i in c: print(i,end='') print() print('字典value的遍历为:',end='') for i in c.values(): print(i,end='') print() print('数组的遍历为:',end='') for i in x: print(i,end='')
输出结果:
列表的遍历为:456132798 元祖的遍历为:12345 字典key的遍历为:小明小红小华 字典value的遍历为:132 数组的遍历为:12345
4、英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 计数字典
- 排序list.sort()
- 输出TOP(10)
s='''Well I wonder could it be When I was dreaming about you baby You were dreaming of me Call me crazy Call me blind To still be suffering is stupid after all of this time Did I lose my love to someone better And does she love you like I do I do, you know I really really do Well hey So much I need to say Been lonely since the day The day you went away So sad but true For me there's only you Been crying since the day the day you went away.!?''' print("do出现次数",s.count('do')) s=s.replace(",","") s=s.replace('.','') s=s.replace('?','') s=s.replace('!','') s=s.replace('\n','') s=s.lower() s1=s.split(' ') key=set(s1) dic={} for i in key: dic[i]=s.count(i) wc=list(dic.items()) wc.sort(key=lambda x:x[1],reverse=True) for i in range(10): print(wc[i])
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY