组合数据类型练习,英文词频统计实例上
Posted 106洪瑜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
dic={‘055‘:80,‘056‘:81,‘057‘:82,‘058‘:83,‘059‘:84,‘060‘:85,‘061‘:86,‘062‘:87,‘063‘:88,‘064‘:89} dic[‘065‘]=90 //增 dic.pop(‘055‘) //删 dic[‘056‘]=91 //改 dic.get(‘070‘,‘不知道‘) //查 ‘070‘ in dic //查 for i in dic: print(i,dic[i]) //遍历
2.列表,元组,字典,集合的遍历。
l=list(‘123456123‘) for i in l: print(i) //列表遍历 t=tuple(‘123456123‘) for i in t: print(i) //元祖遍历 d={‘055‘:80,‘056‘:81,‘057‘:82} for i in d: print(i,d[i]) //字典遍历 s=set(‘123456123‘) for i in s: print(i) //集合遍历
总结列表,元组,字典,集合的联系与区别。
(1)列表是任意对象的序列。列表用方括号表示。
(2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。
(3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。
(4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。字典是Python解释器中最完善的数据类型。
3.英文词频统计实例
- 待分析字符串分解提取单词
- 大小写 txt.lower()
- 分隔符‘.,:;?!-_’
- 单词列表
- 单词计数字典
g=‘‘‘Say something, I‘m giving up on you. I‘ll be the one, if you want me to. Anywhere, I would‘ve followed you. Say something, I‘m giving up on you. And I am feeling so small. It was over my head I know nothing at all. And I will stumble and fall. I‘m still learning to love Just starting to crawl. Say something, I‘m giving up on you. I‘m sorry that I couldn‘t get to you. Anywhere, I would‘ve followed you. Say something, I‘m giving up on you. And I will swallow my pride. You‘re the one that I love And I‘m saying goodbye. Say something, I‘m giving up on you. And I‘m sorry that I couldn‘t get to you. And anywhere, I would have followed you. Oh-oh-oh-oh say something, I‘m giving up on you. Say something, I‘m giving up on you. Say something...‘‘‘ g=g.lower() // 小写 for i in ‘,.‘: g=g.replace(i,‘ ‘) //替换 words=g.split(‘ ‘) //分隔 di = {} words.sort() disc = set(words) for i in disc: di[i] = 0 for i in words: di[i] = di[i]+1 items = di.items() print(items)
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章