组合数据类型练习,英文词频统计实例上
Posted Leheartset
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
dict={\'001\':96,\'002\':89,\'003\':95,\'004\':75} print(\'学生学号成绩:\',dict) dict[\'005\']=93 print(\'增加学号为005的学生的成绩为88,即\',dict) dict.pop(\'003\') print(\'删除学号为003的学生成绩\',dict) print(\'查找学号为003的学生成绩\',dict.get(\'003\')) print(\'查找006的学生成绩\',dict.get(\'006\',\'没有该学生\')) dict[\'004\']=77 print(\'修改学号为004的学生成绩为66\',dict)
运行结果为:
2.列表,元组,字典,集合的遍历。
nums=(\'123456789\') words=(\'abcdefg\') ls=list(\'abcdefg\') s=set(ls) tu=tuple(\'abcdefg\') di=dict(zip(nums,words)) print(di) print(\'打印列表\') for i in ls: print(i) print(\'打印字典\') for j in di: print(j,di[j]) print(\'打印元组\') for i in tu: print(i) print(\'打印集合\') for i in s: print(i)
运行结果为:
总结列表,元组,字典,集合的联系与区别。
答:列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素,区别于元组,可动态增加,删除,更新;元组和列表在结构上没有什么区别,但元组是只读的,不能修改。元组用“()”表示,元组一旦定义其长度和内容都是固定的,即不能对元组进行更新、增加、删除操作;集合内的元素没有重复的元素,通过一个set函数转换成集合,是一个无序不重复元素集;字典存储键值对数据,字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开,字典最大的价值是查询,通过键,查找值,字典和集合都是无序的。
3.英文词频统计实例
str=\'\'\'Waking up I see that everything is okay The first time in my life and now it\'s so great Slowing down I look around and I am so amazed I think about the little things that make life great I wouldn\'t change a thing about it This is the best feeling This innocence is brilliant, I hope that it will stay This moment is perfect, please don\'t go away I need you now And I\'ll hold on to it, don\'t you let it pass you by I found a place so safe, not a single tear The first time in my life and now it\'s so clear Feel calm, I belong, I\'m so happy here It\'s so strong and now I let myself be sincere I wouldn\'t change a thing about it This is the best feeling This innocence is brilliant, I hope that it will stay This moment is perfect, please don\'t go away I need you now And I\'ll hold on to it, don\'t you let it pass you by It\'s the state of bliss you think you\'re dreaming It\'s the happiness inside that you\'re feeling It\'s so beautiful, it makes you wanna cry It\'s the state of bliss you think you\'re dreaming It\'s the happiness inside that you\'re feeling It\'s so beautiful, it makes you wanna cry It\'s so beautiful, it makes you wanna cry This innocence is brilliant, it makes you wanna cry This innocence is brilliant, please don\'t go away \'Cause I need you now And I\'ll hold on to it, don\'t you let it pass you by This innocence is brilliant, it\'s so beautiful, it\'s so beautiful This moment is perfect, please don\'t go away I need you now, it makes me wanna cry And I\'ll hold on to it, don\'t you let it pass you by\'\'\' print(\'待分析字符串:\\n\',str) for i in \',.?!\\n\': str=str.replace(i,\' \') str=str.lower() str=str.split(" ") words=set(str) dt={} dt[\'i\']=str.count(\'i\') dt[\'so\']=str.count(\'so\') dt[\'innocence\']=str.count(\'innocence\') dt[\'you\']=str.count(\'you\') print(\'单词计数字典:\') for j in words: dt[j]=str.count(j) for j in dt: print("{0:<11}{1}".format(j,dt[j]))
运行结果为:
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章