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

练习:

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

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

 

列表用[ ]创建,是可变的数据类型,可以被改变的,而且列表可以嵌套的。元组用()创建,元素之间用“,”分隔,不能修改元组,是不可变的。集合可以用set()函数或者{}创建,元素之间用“,”分隔,不可有重复元素;可以读写,是无序的。字典由key和值values组成;可以用dict()函数或者{}创建,元素之间用“,”分隔,键与值之间用":"分隔;键是唯一的、不可变的,值不要求,是无序的;用key来访问元素。

a = list('457451')      #列表的遍历
print(a)
for i in a:
    print(i)

b = tuple('485890')       #元组的遍历
print(b)
for i in b:
    print(i)

c = set('256156')        #集合的遍历
print(c)
for i in c:
    print(i)

d = {'bob':80,'rose':79,'jack':90}        #字典的遍历
print(d)
for i in d:
    print(i,d[i])

 

 

英文词频统计:

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

 

str = '''I never knew,When the clock stopped and I'm looking at you,
       I never thought I'll miss someone like you,
       Someone I thought that I knew,I never knew,
       I should have known something wouldn't be true,
       Baby you know that I'm so into you,
       More than I know I should do'''
#把单词全部变成小写
a=str.lower()
print(a)
#去掉空格
str=str.lstrip()
print(str)
#将歌词分隔出一个一个的单词list
print("将歌词分隔出一个一个的单词为:")
strList=str.split()
print(strList)
#统计每个单词出现的次数
print("统计每个单词出现的次数为:")
strDict=set(strList)
for word in strDict:
   print(word,strList.count(word))

  

 

posted on 2018-10-08 11:30  zz,ZZ  阅读(151)  评论(0编辑  收藏  举报

导航