python第三次作业

列表,元组,字典,集合的遍历:

1.练习:

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

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

T1=('广州商学院')
print(T1)
for w in T1:
    print(w)
#集合的遍历
S1={1,2,3,4}
print(S1)
for e in S1:
    print(e)
#字典的遍历
di1 = {'y':30,'u':78,'o':40}
print(di1)
for d in di1:
    print(d,di1[d])

2.英文词频统计:

  • 下载一首英文的歌词或文章str
  • 分隔出一个一个的单词 list
  • 统计每个单词出现的次数 dict

      

 

str1='''Well I wonder could it be
  When I was dreaming about you baby
  You were dreaming of me
  Call me crazy
  Call me blind
  To still be suffering is stupid after all of this time

  Did I lose my love to someone better
  And does she love you like I do
  I do, you know I really really do

  Well hey
  So much I need to say
  Been lonely since the day
  The day you went away

  So sad but true
  For me there's only you
  Been crying since the day
  the day you went away

  I remember date and time
  September twenty second
  Sunday twenty five after nine
  In the doorway with your case
  No longer shouting at each other
  There were tears on our faces

  And we were letting go of something special
  Something we'll never have again
  I know, I guess I really really know

  Why do we never know what we've got till it's gone
  How could I carry on the day you went away
  Cause I've been missing you so much I have to say'''
#歌曲中去掉空格
str1=str1.lstrip()
print(str1)
#把单词全部变成小写
a=str1.lower()
print(a)
#将歌词的每个单词分隔
strList=str1.split()
print(len(strList),strList)
#用set把字符串转换成集合
strSet=set(strList)
#计算每个单词出现的次数
for word in strSet:
   print(word,strList.count(word))

 

  

 

  •  

     

 

posted @ 2018-10-08 11:20  asymptote  阅读(194)  评论(0编辑  收藏  举报