组合数据类型练习,英文词频统计实例上
Posted 郑淑莹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
d={\'001\':79,\'002\':82,\'003\':96} print("学生成绩字典:",d) d[\'004\']=78 print("增:",d) d.pop(\'003\') print("删:",d) d[\'001\']=97 print("改:",d)
2.列表,元组,字典,集合的遍历。
s=list(\'24698516645891\') print("列表:",s) for i in s: print(i) t=tuple(\'24698516645891\') print("元组:",t) for i in t: print(i) id={\'001\',\'002\',\'003\'} scores={79,82,95} d=dict(zip(id,scores)) print("字典:",d) for i in d: print(i,d[i]) e=set(\'24698516645891\') print("集合:",e) for i in e: print(i)
总结列表,元组,字典,集合的联系与区别。
答:列表是一种有序的序列,可以随时添加和删除其中的元素,没有限制长度、元素类型可以不同;如果要创建一个集合,需要提供一个列表作为输入集合:不表示集合石有序的,集合是没有重复的,而列表、元组是有重复的;元组和列表非常相似,但是元组一旦初始化就不能修改,,和集合都是内置的有序集合,一个可变,一个不可变;字典使用键值存储,具有极快的查找速度,不会随着键的增加而变慢,需要占用大量的内存,内存浪费多,而列表随着元素的增加而增加,占用空间小,浪费内存很少;所以字典是用空间来换取时间的一种方法。
3.英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符\'.,:;?!-_’
- 单词列表
- 单词计数字典
str=\'\'\'Never look back, we said How was I to know I d miss you so? Loneliness up ahead, emptiness behind Where do I go? And you didn t hear All my joy through my tears All my hopes through my fears Did you know Still I miss you somehow From the bottom of my broken heart There s just a thing or two I d like you to know You were my first love you were my true love From the first kisses tothe very last rose From the bottom of my broken heart Even though time mayfind me somebody new You were my real love I never knew love Til there was you From the bottom of my broken heart Baby,I said, please stay. Give our love a chancefor one more day We could have worked things out Taking time iswhat love is all about But you put a dart Through my dreams through my heart And I m back where I started again Never thought it would end You promised yourself But to somebody else And you made it soperfectly clear Still I wish you were here \'\'\' print("待分析字符串:\\n",str) s=str.lower() print("大写换成小写:",s) for i in \',.?\': t=str.replace(i,\' \') print("删除换行符:\\n",t) d=str.split(\' \') print("分隔:\\n",d) dict={} for i in d: dict[i]=str.count(i) words=list(dict.items()) print("各个单词出现的次数:\\n",words)
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章