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

英文词频统计预备,组合数据类型练习

时间:2017-09-22 13:14:15      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:练习   little   trie   修改   lan   like   转换   hat   friend   

实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。

s = ‘‘‘As Long As You Love Me 
  Athough loneliness has always been a friend of mine.  
I‘m leaving my life in your hands.
People say I‘m crazy and I am blind. 
  Risking it all in a glance. 
  How you got me blind is still a mystery. 
  I can‘t get you out of my head. 
  Don‘t care what is written in your history. 
  As long as you‘re here with me. 
  I don‘t care who you are. 
  Where you‘re from. 
  What you did. 
  As long as you love me. 
  Who you are. 
  Where you‘re from. 
  Don‘t care what you did. 
  As long as you love me. 
  Every little thing that you‘ve said and done. 
  Feels like it‘s deep within me. 
  Doesn‘t really matter if you‘re on the run.
  It seems like we‘re meant to be. 
  I don‘t care who you are. 
  Where you ‘re from. 
  What you did. 
  As long as you love me. 
  Who you are. 
  Where you ‘re from. 
  Don‘t care what you did. 
  As long as you love me. 
  As long as you love me. 
  As long as youlove me. 
  I‘ve tried to hide it so that no one knows.
  But I guess it shows. 
  When you look in to my eyes. 
  What you did and where you‘re coming from. 
  I don‘t care As long as you love me,baby!
  I don‘t care who you are. 
  Where you‘re from. 
  What you did. 
  As long as you love me. 
  Who you are. 
  Where you‘re from. 
  Don‘t care what you did. 
  As long as you love me. 
  Who you are. 
  Where you‘re from. 
  What you did. 
  As long as you love me. 
  Who you are. 
  Where you‘re from. 
  As long as you love me. 
  Who you are.
  As long as you love me. 
  What you did. 
  I don‘t care. 
  As long as you love me.

‘‘‘
a = s.lower()
b = a.replace(., )
c = b.replace(!, )
d = c.replace(?, )
e = d.replace(,, )
f = e.split( )
print(as出现的次数,f.count(as))
print(you出现的次数,f.count(you))
print(long出现的次数,f.count(long))
print(love出现的次数,f.count(love))
print(me出现的次数,f.count(me))

 

 

技术分享

 

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

lis = [1,2,3,2,1,2,3,2]
print(原始的lis=,lis)
lis.append(3)
print(在末尾添加一个3:lis=,lis)
lis.insert(3,2)
print(在下标为3的地方插入一个2,lis=,lis)
del lis[2]
print(删除下标为2的数,lis=,lis)

lis = list(map(int, lis))

print(第一个3的下标为:,lis.index(3))

print(1出现的次数为:,lis.count(1))
print(3出现的次数为:,lis.count(3))

技术分享

 

简要描述列表与元组的异同。

答:列表创建后可以通过语句修改,元组创建后不可修改

英文词频统计预备,组合数据类型练习

标签:练习   little   trie   修改   lan   like   转换   hat   friend   

原文地址:http://www.cnblogs.com/xxxiaoxiannv/p/7574119.html

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