1、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等

score=list('21223113321')
print('作业评分列表:',score)
score.append('3')
print('增加:',score)
score.pop()
print('删除:',score)
score.insert(2,'1')
print('插入:',score)
score[2]='2'
print('修改:',score)
print('第一个3分的下标:',score.index('3'))
print('1分的个数:',score.count('1'))
print('3分的个数:',score.count('3'))

2.字典实例:建立学生学号成绩字典,做增删改查遍历操作

students = {'01':'1','02':'3','03':'2','04':'3','05':'2','06':'1','07':'2'}
ID = students.keys()
mark = students.values()
print("学生信息",students)
print("学生学号",ID)
print("学生成绩",mark)
print("成绩查询:",students.get('05'))
students['07']='4'
print("成绩修改:",students)
students.pop('07')
print("删除学生:",students)
students['08']='3'
print("增加学生:",students)

3、列表,元组,字典,集合的遍历,总结列表,元组,字典,集合的联系与区别

a = list('21456')
b = tuple('12626')
c = {'01':132,'03':2326,'03':51532,'04':2561,'05':5561,'06':521}
d = {2,9,1,4,7,12,8,6}
print("列表的遍历:")
for i in a:print(i,end=' ')
print("\n元组的遍历:")
for i in b:print(i,end=' ')
print("\n字典的遍历:")
for i in c:print(i,":",c.get(i))
print("集合的遍历:")
for i in d:print(i,end=' ')

4、英文词频统计实例

A.待分析字符串

news = '''BERLIN - It's almost certain that German Chancellor Angela Merkel's Union Party will win the most votes in the federal election on Sept 24, but uncertainties still remain concerning the performance of smaller parties, which may play key roles in the formation of a new German government.According to the latest polling, the Union, formed by Merkel's Christian Democratic Union (CDU) and its Bavarian sister Christian Social Union (CSU), are enjoying a comfortable lead over the Social Democratic Party (SPD), with a support rate of around 36 percent versus around 23 percent.German people and media organizations believe that the CDU/CSU will win the most votes and the SPD the second most in the election to be held in less than a week.Despite SPD leader Martin Schulz's spare-no-effort attitude, many German people believe that the SPD lost their last chance to change the game after the bloodless TV debate between Merkel and the former European Parliament president.But the story will not end here, as what's more important is the formation of the new government, in which smaller parties will play key roles, or even serve as kingmakers by joining hands with one of the two big parties.These smaller parties -- the Greens, the Free Democratic Party (FDP), Die Linke (The Left), and the far-right Alternative fuer Deutschland (AfD) -- are in a tight race to become the third largest party in the Bundestag.According to polling results, the support rates of all four of the smaller parties stand at eight to 11 percent.However, voter turnout, uncertain as it is, could still be a game changer in the election. Many people have not yet decided whether they will vote or, if they do, which party they will vote for. Schulz said, perhaps in exaggeration, that almost half of voters had not decided.Among those who are undecided, some believe that Merkel's refugee policies are too radical and don't take into consideration the interests of the German people. However, they do not want to support the anti-immigration AfD either.Some German voters perceive all the parties as having little difference to one another and their policies are converging, thus resulting in their indecision.Voter turnout and swing votes may not change the situation between the CDU/CSU and the SPD, but may largely decide the standings of smaller parties.The German election rule sets a 5-percent-vote hurdle to be elected into the Bundestag, excluding other smaller parties. The German electoral system makes it very difficult for any one party to form a government on its own.'''

B.分解提取单词

a.大小写 txt.lower()

news = open('test.txt','r').read()
news = news.lower()
print("全部小写字母:",news)

b.分隔符'.,:;?!-_’

news = open('test.txt','r').read()
news = news.lower()
for i in '.,:;?!-_':news = news.replace(i,' ')
print("去除分隔符后:",news)

C.计数字典

news = open('test.txt','r').read()
news = news.lower()
for i in '.,:;?!-_':news = news.replace(i,' ')
news = news.split(' ')
dic= {}
keys = set(news)
for i in keys:dic[i] = news.count(i)
print("字典模式:",dic)

D.排序list.sort()

news = open('test.txt','r').read()
news = news.lower()
for i in '.,:;?!-_':news = news.replace(i,' ')
news = news.split(' ')
dic= {}
keys = set(news)
for i in keys:dic[i] = news.count(i)
wc = list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
print("出现次数排列:",wc)

E.输出TOP(10)

news = open('test.txt','r').read()
news = news.lower()
for i in '.,:;?!-_':news = news.replace(i,' ')
news = news.split(' ')
dic= {}
keys = set(news)
for i in keys:dic[i] = news.count(i)
wc = list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
print("出现次数最多的10个单词:")
for i in range(10):print(wc[i])

F.去除部分单词

news = open('test.txt','r').read()
exc = {'the','a','of','is','are','to','and','in','will','not','as','that','they',''}
news = news.lower()
for i in '.,:;?!-_':news = news.replace(i,' ')
news = news.split(' ')
dic= {}
keys = set(news)
for i in exc:keys.remove(i)
for i in keys:dic[i] = news.count(i)
wc = list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
print("出现次数最多的10个主要单词:")
for i in range(10):print(wc[i])

转载于:https://www.cnblogs.com/djl1995/p/7571030.html

组合数据类型练习,英文词频统计实例相关推荐

  1. 组合数据类型,英文词频统计

    练习: 1.总结列表,元组,字典,集合的联系与区别. 列表 [,] 有序,可变,值可以重复 元组(,) 有序,不可修改,不可重复 集合可以用set()函数或者{}创建 用,分隔,不可有重复元素,是无序 ...

  2. 【作业】组合数据类型练习,英文词频统计实例

    1.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作.例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等. 1 score = list('012332211') ...

  3. 组合数据类型练习,英文词频统计实例9-21

    1.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作.例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等. >>>score=list('212 ...

  4. 组合数据类型练习,英文词频统计实例上(2017.9.22)

    字典实例:建立学生学号成绩字典,做增删改查遍历操作. sno=['33号','34号','35号','36号'] grade=[100,90,80,120] d={'33号':100,'34号':90 ...

  5. 组合数据类型练习,英文词频统计实例上

    1.name=['陈楠芸','陈文琪','刘书签','杨必须'] scores=[7,6,6,5] d={'陈楠芸':7,'陈文琪':6,'刘书签':6,'杨必须':5} print(d) #增加 d ...

  6. 复合数据类型,英文词频统计

    这次作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753 1.列表,元组,字典,集合分别如何增删改查及遍历. (1)列表 ...

  7. 文件方式实现完整的英文词频统计实例

    可以下载一长篇的英文小说,进行词频的分析. 1.读入待分析的字符串 2.分解提取单词 3.计数字典 4.排除语法型词汇 5.排序 6.输出TOP(20) 7.对输出结果的简要说明. fo=open(' ...

  8. 文件方式实现完整的英文词频统计实例(9.27)

    1.读入待分析的字符串 2.分解提取单词 3.计数字典 4.排除语法型词汇 5.排序 6.输出TOP(20) 文本代码如下: girl='''Remembering me, Discover and ...

  9. 064文件方式实现完整的英文词频统计实例

    fr = open('test.txt' , 'r',encoding='utf-8')s = fr.read() s=s.lower()s=s.replace('\n',' ')word=s.spl ...

最新文章

  1. linux 跟踪程序执行过程,用pvtrace和Graphviz实现对linux下C程序的函数调用跟踪
  2. centos7安装单节点mysql(源码包安装)
  3. 【C】printf按8进制、10进制、16进制输出以及高位补0
  4. 【最新版】Java速成路线(急于找工作!)
  5. 剑指Offer - 面试题46. 把数字翻译成字符串(DP)
  6. 2019字节跳动秋招笔试
  7. form必填默认校验_Salesforce LWC学习(十六) Validity 在form中的使用浅谈
  8. solr 配置中文分析器/定义业务域/配置DataImport功能(测试用)
  9. 网络触发的detach
  10. OKRA-ERP简单实用产能分析
  11. 为什么机会总是留给有准备的人?这是我听过最好的答案
  12. 【转】为什么要使用ModelDriven
  13. linux下建立软链接
  14. php artisan 常用命令,php artisan module常用命令
  15. css flex 文字右对齐,css flex align-items属性 交叉轴上对齐方式垂直对齐方式
  16. java wav转amr_AMR和WAV互相转换
  17. 竞赛经验——挑战杯、互联网加、北斗杯、微软创新杯、计算机设计等比赛教训与经验
  18. 数据中心甲方项目管理杂谈
  19. 【免费素材】必备国内外常用blender材质模型下载网站
  20. 搭建实验室3d slam 移动小车 3.1jackal移动小车平台调试

热门文章

  1. git不能push文件
  2. 感谢谦哥的家族为中国相声事业做出了贡献。
  3. i春秋 429-线上赛题(一)Writeup
  4. 浅谈如何进行网站结构优化
  5. excel快速把公式应用到一整列
  6. Python(arcpy) 根据站点经纬度(坐标)批量提取对应格点值
  7. Chrome 源码剖析
  8. node.js云学堂微信小程序学习系统的设计与实现毕业设计源码011735
  9. 古代日本人没有姓,只有名
  10. 数据库的横向和纵向分表