组合数据类型练习,英文词频统计实例
列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
1 fen = list('112332111223322331132233112233') 2 print("第一个3分的下标为:",fen.index('3')) 3 print("得到3分的人数为:",fen.count('3')) 4 print("得到1分的人数为:",fen.count('1')) 5 fen.append('5') 6 print("尾部增加:",fen) 7 fen.insert(2,'6') 8 print("下标插入:",fen) 9 fen.pop() 10 print("尾部删除:",fen) 11 fen.pop(2) 12 print("下标删除:",fen)
字典实例:建立学生学号成绩字典,做增删改查遍历操作。
1 mask = dict (zip('123456789','123321123')) 2 print(mask) 3 print("成绩查询:",mask.get('5')) 4 mask['4']='1' 5 print("成绩修改:",mask) 6 mask.pop('9') 7 print("删除学生:",mask) 8 mask['10']='3' 9 print("增加学生:",mask)
列表,元组,字典,集合的遍历。
1 list1=list('adffedvfdgvcbvuyzwgh') 2 tuple1=tuple('48685343867') 3 d = dict (zip('wertyuiop','3217895640')) 4 s = set(list1) 5 print("列表:",list1) 6 for i in list1: 7 print(i,end=' ') 8 print("\n") 9 print("元组:",tuple1) 10 for i in tuple1: 11 print(i,end=' ') 12 print("\n") 13 print("字典:",d) 14 for i in d: 15 print(i,end='\t') 16 print(d[i],end='\n') 17 print("集合:") 18 for i in s: 19 print(i,end=' ')
英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 计数字典
-
排除语法型词汇,代词、冠词、连词
-
- 排序list.sort()
- 输出TOP(10)
1 of=open('young.txt','w') 2 of.write('''We Are Young 3 from Fun. 4 by Shannon Yvox 5 Give me a second I 6 I need to get my story straight 7 My friends are in the bathroom 8 Getting higher than the Empire State 9 My lover she's waiting for me 10 Just across the bar 11 My seats been taken by some sunglasses 12 Asking 'bout a scar 13 And I know I gave it to you months ago 14 I know you're trying to forget 15 But between the drinks and subtle things 16 The holes in my apologies 17 You know I'm trying hard to take it back 18 So if by the time the bar closes 19 And you feel like falling down 20 I'll carry you home 21 Tonight 22 We are young 23 So let's set the world on fire 24 We can burn brighter 25 Than the sun 26 Tonight 27 We are young 28 So let's set the world on fire 29 We can burn brighter 30 Than the sun 31 Now I know that I'm not 32 All that you got 33 I guess that I 34 I just thought maybe we could find new ways to fall apart 35 But our friends are back 36 So let's raise a cup 37 Cause I found someone to carry me home 38 Tonight 39 We are young 40 So let's the set the world on fire 41 We can burn brighter 42 Than the sun 43 Tonight 44 We are young 45 So let's set the world on fire 46 We can burn brighter 47 Than the sun 48 Carry me home tonight 49 Just carry me home tonight 50 Carry me home tonight 51 Just carry me home tonight 52 The world is on my side 53 I have no reason to run 54 So will someone come and carry me home tonight 55 The angels never arrived 56 But I can hear the choir 57 So will someone come and carry me home 58 Tonight 59 We are young 60 So let's set the world on fire 61 We can burn brighter 62 Than the sun 63 Tonight 64 We are young 65 So let's set the world on fire 66 We can burn brighter 67 Than the sun 68 So if by the time the bar closes 69 And you feel like falling down 70 I'll carry you home tonight''') 71 of.close() 72 fo=open('young.txt','r') 73 sw=fo.read() 74 sw=sw.lower() 75 exc={'the','in','to','and','if','are'} 76 for i in '''\t\\\'\"''': 77 sw=sw.replace(i,' ') 78 79 words=sw.split(' ') 80 81 s=set (words) 82 for j in exc: 83 s.remove(j) 84 number={} 85 for i in s: 86 number[i]=words.count(i) 87 88 listnumber=list (number.items()) 89 90 listnumber.sort(key=lambda x:x[1],reverse=True) 91 for i in range(10): 92 print(listnumber.pop(i))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)