码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-21 19:17:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:list   insert   ceo   ast   png   children   word   fast   alt   

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

score = list(012332211)
print(分数为:,score)
print(1分的同学个数:,score.count(1))
print(3分的同学个数:,score.count(3))
print(第一个3分同学的下标为:,score.index(3))
score.append(0)
print(添加一个0分在表的末尾:,score)
score.insert(0,3)
print(添加一个3分在表的开头:,score)
score.pop(1)
print(删除第二个分数:,score)

技术分享

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

d={:95,:86,:75,:84,:96}
print(学生成绩字典,d)
d[]=87
print(增加一个学生)
print(d)
d.pop()
print(删除学生孙)
print(d)
d[]=97
print(修改学生吴的成绩)
print(d)
print(查找学生赵的成绩:,d.get())

技术分享

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

 ls = list(123456789)
  ln = tuple(123456789)
  s = {01:100,02:99,03:98,04:97,05:96,05:96,06:95,07:98,08:90,09:91}
  a= set(123456789)
  print(列表:,ls)
  print(元组,ln)
  print(字典,s)
  print(集合,a)
  print(\n列表的遍历:)
 for i in ls:
     print(i, ,end=  )
 print(\n元组的遍历:)
 for j in ln:
     print(j, ,end=  )
 print(\n字典的遍历:)    
 for k in s:
     print(k,‘‘,end=  )
 print(\n集合的遍历:)    
 for l in a:
     print(l,‘‘,end=  )

技术分享

4、英文词频统计实例

待分析字符串分解提取单词

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符‘.,:;?!-_’
  3. 计数字典
    1. 排除语法型词汇,代词、冠词、连词

  4. 排序list.sort()
  5. 输出TOP(10)
news=‘‘‘Its normal for fast food brands to poke fun at their competitors,
but Panera Breads latest social media campaign isnt cracking jokes.
Its a call for executives everywhere to put their kiddie meals where their
mouth is.
On Wednesday, Panera Breads founder and CEO, Ron Shaich, launched
the #KidsMenuChallenge, a social media campaign that challenges other fast food
executives to spend a week eating the food thats served on their own childrens
menus.‘‘‘
print(新闻:,news)

news=news.lower()#小写
print(小写:,news)

for i in ,.:#将符号替换成空格
     news=news.replace(i, )
print(符号换成空格:,news)

words=news.split( )#用空格分解每一个单词

dic={}#字典
keys=set(words)#出现的所有单词的集合,字典的key
exc={"a","to","is","at","the",‘‘}#没有意义的、不要的单词
for w in exc:#去掉没有意义的单词
     keys.remove(w)
for i in keys:#记录次数
    dic[i]=words.count(i)
wc=list(dic.items())#列表
wc.sort(key=lambda x:x[1],reverse=True)
print(记录次数:)
for i in range(10):#显示10个单词及其次数
     print(wc[i])

技术分享

5、文本操作

fo=open(/Users/Administrator/Desktop/test.txt,r)
news=fo.read()
fo.close()
exc={the,a,to,of,and,on,in,that}
news =news.lower()
for i in ‘‘‘,.?!"‘‘‘:
    news=news.replace(i, )

print(news)
words=news.split( )
print(words)
d={}
keys = set(words)
for r in exc:
    keys.remove(r)
for i in keys:
    d[i]=words.count(i)
wc=list(d.items())
wc.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    print(wc[i])

 

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

标签:list   insert   ceo   ast   png   children   word   fast   alt   

原文地址:http://www.cnblogs.com/xujiekai/p/7569990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!