猪精雅0

导航

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

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

d = {'Bob': 75, 'Michael': 95, 'Tracy': 85}
print(d)

print('查找到Michael的成绩是:',d['Michael'])

print('是否存在Thomas的成绩:','Thomas' in d)

d.pop('Bob')
print('删除Bob的成绩:',d)                  

print('所有的学生姓名为:',d.keys())

print('所有的学生成绩为:',d.values())

print('所有学生的姓名和成绩为:',d.items())

d['Michael']=100
print('将Michael的成绩修改为100:',d)
print(d)

del(d['Tracy'])

print('删除Tracy的信息',d)

 

 

2.列表,元组,字典,集合的遍历。

总结列表,元组,字典,集合的联系与区别。

 

list = [1,2,3,4,5,5,4,3,2,1]
print('list的遍历为:')
for i in list:
    print(i)

tup = (1,2,3,4,4,3,2,1)
print('tup的遍历为:')
for j in tup:
    print(j)

dict = {1,2,3,4,4,3,2,1}
print('dict的遍历为:')
for k in dict:
    print(k)

 

 

3.英文词频统计实例

待分析字符串

分解提取单词

大小写 txt.lower()

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

单词列表

单词计数字典

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.

'''

s = s.lower()
print('-------------------------------歌词列表--------------------------------')
for i in (',.!?\n'):
s=s.replace(i,' ')
s=s.split()
s=list(s)
print(s)
print('---------------------歌词中每个单词以及其出现的次数------------------------')
a = Counter(s)
print(a.items())

 

 

 

posted on 2017-09-26 16:02  102林晓霞  阅读(116)  评论(0)    收藏  举报