组合数据类型练习,英文词频统计实例
Posted 26黄培康
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 >>>
list:是一种有序的序列,可以随时添加和删除,没有长度限制等;
tuple:和list非常相似;
dict:使用键-值key-value储存,有极快的查找速度,其中的key必须是不可变对象,和list相比dict查找和插入的速度极快,不会因key的增加而变慢但是要占用大量的内存造成浪费,是一种用空间换取时间的方法;
set:是一组key的集合,不储存value,没有重复的key,创建一个set需要提供一个list作为输入集合,不一定是有序的。
4、英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符‘.,:;?!-_’
- 计数字典
- 排序list.sort()
- 输出TOP(10)
news=‘‘‘A Chinese company offering sex dolls for rent has withdrawn its services just days after launching. Touch had begun offering five different sex doll types for daily or longer-term rent on Thursday in Beijing but quickly drew complaints and criticism. The company said in a statement on Weibo it "sincerely apologised for the negative impact" of the concept. But the firm stressed sex was "not vulgar" and said it would keep working towards more people enjoying it. Touch told the BBC the rental service had operated for two days. "We prepared ten dolls for the trial operation," a company spokesperson said via email, adding that they received very positive feedback from users. "But it’s really hard in China," the firm wrote, saying there had been a lot of controversy with the police over the issue. The company had offered the sex dolls for a daily fee of 298 yuan ($46), according to Chinese media. The models on offer were marketed as Chinese, Korean and Russian women, with one also modelled on the movie character Wonder Woman, complete with a sword and shield. In its Weibo statement, the firm said its original intention had been to make expensive silicone dolls more affordable but conceded that the service triggered a heated public debate. The company also said it would pay out compensation to users worth double the amount they had paid as a deposit for reserving a doll. The statement added that Touch would in future pay more attention to its "social duty", and would actively promote a "healthier and more harmonious sex lifestyle". Aside from its short-lived rental offering, the firm sells an array of sex toys, including sex dolls.‘‘‘ news= news.lower() for i in ‘,./-_"":;‘: news=news.replace(i,‘ ‘) words=news.split(‘ ‘) dict={} keys=set(words) for i in keys: dict[i]=words.count(i) tj=list(dict.items()) tj.sort(key=lambda x:x[1],reverse=True) for i in tj: print(i)
结果:
>>> RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/统计.py (‘‘, 23) (‘the‘, 15) (‘a‘, 10) (‘sex‘, 7) (‘for‘, 7) (‘and‘, 6) (‘had‘, 6) (‘company‘, 5) (‘said‘, 5) (‘its‘, 5) (‘dolls‘, 5) (‘\n\nthe‘, 5) (‘firm‘, 4) (‘would‘, 4) (‘to‘, 4) (‘of‘, 4) (‘in‘, 4) (‘on‘, 4) (‘it‘, 4) (‘more‘, 4) (‘but‘, 3) (‘statement‘, 3) (‘that‘, 3) (‘with‘, 3) (‘chinese‘, 3) (‘offering‘, 3) (‘rent‘, 2) (‘been‘, 2) (‘as‘, 2) (‘they‘, 2) (‘from‘, 2) (‘doll‘, 2) (‘days‘, 2) (‘pay‘, 2) (‘rental‘, 2) (‘service‘, 2) (‘users‘, 2) (‘also‘, 2) (‘weibo‘, 2) (‘\n\ntouch‘, 2) (‘\n\n‘, 2) (‘daily‘, 2) (‘toys‘, 1) (‘social‘, 1) (‘quickly‘, 1) (‘including‘, 1) (‘adding‘, 1) (‘apologised‘, 1) (‘types‘, 1) (‘positive‘, 1) (‘deposit‘, 1) (‘told‘, 1) (‘after‘, 1) (‘fee‘, 1) (‘term‘, 1) (‘intention‘, 1) (‘criticism‘, 1) (‘trial‘, 1) (‘very‘, 1) (‘enjoying‘, 1) (‘keep‘, 1) (‘lifestyle‘, 1) (‘sincerely‘, 1) (‘not‘, 1) (‘according‘, 1) (‘working‘, 1) (‘or‘, 1) (‘triggered‘, 1) (‘women‘, 1) (‘operated‘, 1) (‘attention‘, 1) (‘wrote‘, 1) (‘controversy‘, 1) (‘actively‘, 1) (‘towards‘, 1) (‘\n\naside‘, 1) (‘over‘, 1) (‘issue‘, 1) (‘just‘, 1) (‘were‘, 1) (‘amount‘, 1) (‘offered‘, 1) (‘shield‘, 1) (‘added‘, 1) (‘there‘, 1) (‘lot‘, 1) (‘sword‘, 1) (‘operation‘, 1) (‘affordable‘, 1) (‘298‘, 1) (‘beijing‘, 1) (‘($46)‘, 1) (‘police‘, 1) (‘make‘, 1) (‘harmonious‘, 1) (‘concept‘, 1) (‘out‘, 1) (‘short‘, 1) (‘worth‘, 1) (‘promote‘, 1) (‘impact‘, 1) (‘negative‘, 1) (‘vulgar‘, 1) (‘expensive‘, 1) (‘lived‘, 1) (‘was‘, 1) (‘two‘, 1) (‘healthier‘, 1) (‘models‘, 1) (‘array‘, 1) (‘longer‘, 1) (‘launching‘, 1) (‘has‘, 1) (‘feedback‘, 1) (‘russian‘, 1) (‘silicone‘, 1) (‘begun‘, 1) (‘reserving‘, 1) (‘services‘, 1) (‘email‘, 1) (‘modelled‘, 1) (‘\n\nbut‘, 1) (‘korean‘, 1) (‘offer‘, 1) (‘marketed‘, 1) (‘woman‘, 1) (‘media‘, 1) (‘hard‘, 1) (‘future‘, 1) (‘wonder‘, 1) (‘prepared‘, 1) (‘touch‘, 1) (‘one‘, 1) (‘double‘, 1) (‘yuan‘, 1) (‘stressed‘, 1) (‘public‘, 1) (‘spokesperson‘, 1) (‘china‘, 1) (‘bbc‘, 1) (‘original‘, 1) (‘character‘, 1) (‘we‘, 1) (‘paid‘, 1) (‘really‘, 1) (‘drew‘, 1) (‘sells‘, 1) (‘withdrawn‘, 1) (‘movie‘, 1) (‘it’s‘, 1) (‘five‘, 1) (‘duty‘, 1) (‘via‘, 1) (‘compensation‘, 1) (‘\n\nin‘, 1) (‘an‘, 1) (‘different‘, 1) (‘complete‘, 1) (‘people‘, 1) (‘thursday‘, 1) (‘conceded‘, 1) (‘complaints‘, 1) (‘debate‘, 1) (‘saying‘, 1) (‘ten‘, 1) (‘heated‘, 1) (‘received‘, 1) >>>
以上是关于组合数据类型练习,英文词频统计实例的主要内容,如果未能解决你的问题,请参考以下文章