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

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

列表用[ ]创建,是可变的数据类型,可以被改变的,而且列表可以嵌套的。

元组用()创建,元素之间用“,”分隔,不能修改元组,是不可变的。

集合可以用set()函数或者{}创建,元素之间用“,”分隔,不可有重复元素;可以读写,是无序的。

字典由key和值values组成,可以用dict()函数或者{}创建,元素之间用“,”分隔,键与值之间用":"分隔;键是唯一的、不可变的,值不要求,是无序的;用key来访问元素。

 

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

#列表的遍历
List1=['Tracy',20,'Amy',10]
print(List1)
for a in List1:
    print(a)
#元组的遍历
Tuple1=('今天天气真好')
print(Tuple1)
for b in Tuple1:
    print(b)
#集合的遍历
Set1={1,2,3,4}
print(Set1)
for c in Set1:
    print(c)
#字典的遍历
dict1 = {'a':80,'b':79,'c':90}
print(dict1)
for d in dict1:
    print(d,dict1[d])

结果:

 

 

 

英文词频统计

str1='''Meet me on the other side
Meet me on the other side
I'll see you on the other side
See you on the other side
Honey now if I'm honest
I still don't know what love is
Another mirage folds into the haze of time recalled
And now the floodgates cannot hold
All my sorrow all my rage
A tear that falls on every page
Meet me on the other side
Meet me on the other side
Maybe I oughta mention
Was never my intention
To harm you or your kin
Are you so scared to look within
The ghosts are crawling on our skin
We may race and we may run
We'll not undo what has been done
Or change the moment when it's gone
Meet me on the other side
Meet me on the other side
I'll see you on the other side
I'll see you on the other side
I know it would be outrageous
To come on all courageous
And offer you my hand
To pull you up on to dry land
When all I got is sinking sand
The trick ain't worth the time it buys
I'm sick of hearing my own lies
And love's a raven when it flies
Meet me on the other side
Meet me on the other side
I'll see you on the other side
Honey now if I'm honest
I still don't know what love is'''
#歌曲中去掉空格
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-11 21:18  狮大大丶  阅读(139)  评论(0)    收藏  举报