组合数据类型练习,英文词频统计实例
Posted 百里守约
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例相关的知识,希望对你有一定的参考价值。
1.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
score = list(\'222331132\') print(\'分数为:\'),score print(\'得1分的同学个数:\'),score.count(\'1\') print(\'得3分的同学个数:\'),score.count(\'3\') print(\'第一个3分同学的下标为:\'),score.index(\'3\') score.append(\'0\')
2.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
>>> d={\'09\':\'66\',\'05\':\'80\',\'14\':\'78\',\'23\':\'90\'} >>> d[\'23\'] \'90\' >>> d.keys() dict_keys([\'09\', \'05\', \'14\', \'23\']) >>> d.values() dict_values([\'66\', \'80\', \'78\', \'90\']) >>> d.items() dict_items([(\'09\', \'66\'), (\'05\', \'80\'), (\'14\', \'78\'), (\'23\', \'90\')]) >>> d.get(\'05\') \'80\' >>> print(d.get(\'01\',\'00\')) 00 >>> d {\'09\': \'66\', \'05\': \'80\', \'14\': \'78\', \'23\': \'90\'} >>> d[\'01\']=\'11\' >>> d {\'09\': \'66\', \'05\': \'80\', \'14\': \'78\', \'23\': \'90\', \'01\': \'11\'} >>> del(d[\'01\']) >>> d {\'09\': \'66\', \'05\': \'80\', \'14\': \'78\', \'23\': \'90\'}
3、列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。
l=list(\'1101010101\') t=tuple(\'1101010101\') s=set(\'1101010101\') d=dict(zip(\'abcd\',[1,2,3,4])) print(\'列表\',l) for i in l: print(i,end=\' \') print(\'\\n\') print(\'元组\',t) for i in t: print(i,end=\' \') print(\'\\n\') print(\'集合\',s) for i in s: print(i,end=\' \') print(\'\\n\') print(\'字典\',d) for i in d: print(i,d[i])
4、英文词频统计实例
f=open(\'E:/作业/2017课程考核/Python/red.txt\') songs=f.read() f.close() songs =songs.replace("\\n",\' \') exc={\'the\',\'a\',\'to\',\'of\',\'and\',\'in\',\'that\',\'on\'} songs=songs.lower() for i in \',…\': songs=songs.replace(i,\' \') print(songs) words=songs.split(\' \') keys=set(words) for w in exc: keys.remove(w) dic={} for i in keys: dic[i]=words.count(i) wc=list(dic.items()) wc.sort(key=lambda x:x[1],reverse=True) for j in range(10): print(wc[j])
待分析字符串
Loving him is like driving a new maserati down a dead end street Faster than the wind, passionate as sin, ending so suddenly Loving him is like trying to change your mind once you\'re already flying through the free fall Like the colors in autumn so bright just before they lose it all Losing him was blue, like I\'d never known Missing him was dark gray, all alone Forgetting him was like trying to know somebody you\'ve never met But loving him was red Loving him was red Touching him was like realizing all you ever wanted was right there in front of you Memorizing him was as easy as knowing all the words to your old favorite song Fighting with him was like trying to solve a crossword and realizing there\'s no right answer Regretting him was like wishing you never found out that love could be that strong Losing him was blue, like I\'d never known Missing him was dark gray, all alone Forgetting him was like trying to know somebody you never met But loving him was red, oh red, burning red Remembering him comes in flashbacks and echoes Tell myself it\'s time now gotta let go But moving on from him is impossible when I still see it all in my head…In burning red Burning, it was red Losing him was blue, like I\'d never known Missing him was dark gray, all alone Forgetting him was like trying to know somebody you\'ve never met Cause loving him was red, yeah yeah red, burning red And that’s why he’s spinning around in my head Comes back to me in burning red Loving him is like driving a new maserati down a dead end street
分解提取单词
大小写 txt.lower()
分隔符\'.,:;?!-_’
计数字典
排除语法型词汇,代词、冠词、连词
排序list.sort()
输出TOP(10)
以上是关于组合数据类型练习,英文词频统计实例的主要内容,如果未能解决你的问题,请参考以下文章