组合数据类型练习,英文词频统计实例
1.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
f=list('123223121321312') print('成绩表:',f) f.append('3') print('增加一个同学的分数后') print('成绩表:',f) f.pop(2) print('删除下标为2的同学的分数后') print('成绩表:',f) f[3] = 1 print('修改下标为3的同学的分数') print('成绩表:',f) x=f.index('3') print('查找第一个3分的下标:{}'.format(x)) y=f.count('1') print('1分的人数有{}个人'.format(y)) s=f.count('3') print('3分的人数有{}个人'.format(s))
2.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
d={'赵':95,'钱':86,'孙':75,'李':84,'周':96} print('学生成绩字典',d) d['吴']=87 print('增加一个学生') print(d) d.pop('孙') print('删除学生孙') print(d) d['吴']=97 print('修改学生吴的成绩') print(d) print('查找学生赵的成绩:',d.get('赵'))
3.列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。
l=list('1233332121') t=tuple('654321123456') x={'201720':'88','201721':'90','201722':'78'} s=set('123321332122') print("列表:",l) for i in l: print(i,end=' ') print("\n") print("元组:",t) for i in t: print(i,end=' ') print("\n") print("字典:",x) for i in x: print(i,end='\t') print(x[i],end='\n') print("集合:",) for i in s: print(i,end=' ')
4.英文词频统计实例
待分析字符串分解提取单词
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 计数字典
-
排除语法型词汇,代词、冠词、连词
-
- 排序list.sort()
- 输出TOP(10)
-
news='''It's normal for some high calorie food which like fast food brands to poke fun at their competitors, howerver, Panera Bread's latest social media campaign isn't cracking jokes. It's a call for executives everywhere to put their kiddie meals where their mouth is. On Wednesday, Panera Bread's founder and CEO, Ron Shaich, launched the #KidsMenuChallenge, a social media campaign that challenges other fast food executives to spend a week eating the food that's served on their own children's menus.''' print('新闻:',news) news=news.lower() print('小写:',news) for i in ',.': news=news.replace(i,' ') print('符号换成空格:',news) words=news.split(' ') dic={} keys=set(words) exc={"a","to","is","at","the",''} for w in exc: keys.remove(w) for i in keys: dic[i]=words.count(i) wc=list(dic.items()) wc.sort(key=lambda x:x[1],reverse=True) print('记录次数:') for i in range(10): print(wc[i])