组合数据类型练习,英文词频统计实例上
Posted xy223
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
dict={\'02\':89,"03":90,"04":100,"05":75} print("查询02学号的成绩") print(dict.get(\'02\')) print("删除03号的记录") dict.pop(\'03\') print(dict.items()) print("01学号的成绩改为99") dict[\'01\']=99 print(dict.items()) print("添加06号学生") dict["06"]=80 print("查找:") print(dict.get(\'05\',\'不存在\')) print(dict.get(\'10\',\'不存在\')) print(dict.items()) print("遍历") for i in dict: print(i)
2.列表,元组,字典,集合的遍历。 总结列表,元组,字典,集合的联系与区别。
lis=["01","02","03","04"] tu=("01","02","03","04","05") dic={"01":"91","02":"92","03":"93","04":"94"} s=set(lis) for i in lis: print(i) for i in tu: print(i) for i in dic: print(\'{0:<}:{1:>}\'.format(i,dic[i])) for i in s: print(i)
答:列表是有序的元素集合,元组与列表很相像,但是最大的区别就是元组只能被读取而不能被修改。字典是以键值对作为单位来进行存放的集合。集合(set)则是一组key的无序集合,但是它只包含key,key值不能够重复,并且需要先创建一个List作为输入集合才能继续创建set。
3.英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符\'.,:;?!-_’
- 单词列表
- 单词计数字典
ordinary=\'\'\'This is just an ordinary day, Wipe the insecurities away. I can see that the darkness will erode, Looking out the corner of my eye,. I can see that the sunshine will explode, Far across the desert in the sky. Beautiful girl won\'t you be my inspiration, Beautiful girl don\'t you throw your love around. What in the world, what in the world could ever come between us, beautiful girl, beautiful girl. I\'ll never let you down, won\'t let you down, This is the beginning of your day. Life is more intricate than it seems, Always be yourself along the way. Living through the spirit of your dreams.\'\'\' ordinary=ordinary.lower() for i in \',.\\"?\': ordinary=ordinary.replace(i,\' \') words=ordinary.split(\' \') print(words) dict={} for i in words: dict[i]=words.count(i) print(dict)
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章