组合数据类型练习,英文词频统计实例
Posted ELsky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例相关的知识,希望对你有一定的参考价值。
1、由字符串创建一个作业评分表,做增删改查询统计遍历操作,例如查询第一个3分的下标,统计1分的同学有几个,3分的同学有几个,增删改查等等。
>>> fenshu = list(\'1213223131312232323\') >>> fenshu [\'1\', \'2\', \'1\', \'3\', \'2\', \'2\', \'3\', \'1\', \'3\', \'1\', \'3\', \'1\', \'2\', \'2\', \'3\', \'2\', \'3\', \'2\', \'3\'] >>> fenshu.index(\'3\') 3 >>> fenshu.count(\'1\') 5 >>> fenshu.count(\'3\') 7 >>> fenshu.append(\'1\') >>> fenshu [\'1\', \'2\', \'1\', \'3\', \'2\', \'2\', \'3\', \'1\', \'3\', \'1\', \'3\', \'1\', \'2\', \'2\', \'3\', \'2\', \'3\', \'2\', \'3\', \'1\'] >>> fenshu.insert(1,\'3\') >>> fenshu [\'1\', \'3\', \'2\', \'1\', \'3\', \'2\', \'2\', \'3\', \'1\', \'3\', \'1\', \'3\', \'1\', \'2\', \'2\', \'3\', \'2\', \'3\', \'2\', \'3\', \'1\'] >>> fenshu.pop() \'1\' >>> fenshu.pop(3) \'1\' >>> fenshu [\'1\', \'3\', \'2\', \'3\', \'2\', \'2\', \'3\', \'1\', \'3\', \'1\', \'3\', \'1\', \'2\', \'2\', \'3\', \'2\', \'3\', \'2\', \'3\'] >>>
2、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
>>> k={\'201406114326\':\'3\',\'201406114327\':\'2\',\'201406114328\':\'1\',\'201406114329\':\'0\'} >>> k[\'201406114326\'] \'3\' >>> k.pop(\'201406114327\') \'2\' >>> k {\'201406114326\': \'3\', \'201406114328\': \'1\', \'201406114329\': \'0\'} >>> k.keys() dict_keys([\'201406114326\', \'201406114328\', \'201406114329\']) >>> k.values() dict_values([\'3\', \'1\', \'0\']) >>> k.items() dict_items([(\'201406114326\', \'3\'), (\'201406114328\', \'1\'), (\'201406114329\', \'0\')]) >>> k.get(\'201406114326\') \'3\' >>> k.get(\'201406114327\',\'无结果\') \'无结果\' >>>
3、列表,元组,字典,集合的遍历,总结列表,元组,字典,集合的联系与区别。
>>> fenshu=list(\'32123123123\') >>> zd=tuple(\'32123123123\') >>> k={\'201406114326\':\'3\',\'201406114327\':\'2\',\'201406114328\':\'1\',\'201406114329\':\'0\'} >>> s=set(\'32123123123\') >>> fenshu [\'3\', \'2\', \'1\', \'2\', \'3\', \'1\', \'2\', \'3\', \'1\', \'2\', \'3\'] >>> zd (\'3\', \'2\', \'1\', \'2\', \'3\', \'1\', \'2\', \'3\', \'1\', \'2\', \'3\') >>> k {\'201406114326\': \'3\', \'201406114327\': \'2\', \'201406114328\': \'1\', \'201406114329\': \'0\'} >>> s {\'3\', \'2\', \'1\'} >>> for i in fenshu: print(i,end=\'\') 32123123123 >>> for i in zd: print(i,end=\'\') 32123123123 >>> for i in k: print(i) 201406114326 201406114327 201406114328 201406114329 >>> for i in s: print(i) 3 2 1 >>>
4.词频统计
news=\'\'\'My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment. \'\'\' news=news.lower() for i in \',.\': news=news.replace(i,\' \') words=news.split(\' \') dict={} keys=set(words) for i in words: dict[i]=words.count(i) count=list(dict.items()) count.sort(key=lambda x:x[1],reverse=True) for i in range(10): print(count[i])
以上是关于组合数据类型练习,英文词频统计实例的主要内容,如果未能解决你的问题,请参考以下文章