第3次 组合数据类型,英语词频统计

#定义一个列表
classmates =['lucy','Bob','amy','陈四','tom',65]
new = ['Tim','Chenjie']
#向classmates中下标为2处插入一个元素
classmates.insert(2,'Wuyifan')
#使用append向末尾增加一个元素
classmates.append('Chenjie')
classmates.extend(new)
print(classmates)
classmatesSet = set(classmates)

classmatesSet.add('Chenjie')
print(classmatesSet)
print(list('acadswet'),tuple('ababcd'))

 

 

#列表的遍历
List1=['Tim',30,'Tom',20]
print(List1)
for one in List1:
    print(one)
#元组的遍历
Tuple1=('我要当歌手!')
print(Tuple1)
for two in Tuple1:
    print(two)
#集合的遍历
Set1={4,5,6,7}
print(Set1)
for three in Set1:
    print(three)
#字典的遍历
dict1 = {'a':10,'b':20,'c':30}
print(dict1)
for four in dict1:
    print(four,dict1[four])

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

列表:有序,可做增删改查操作,用方括号[x,y,z]的方式表示

元组:有序,不可做修改操作,用小括号(x,y,z)的方式表示

字典:无序,可做增删改查操作,其中组成元素为键值对,用花括号{a:b,c:d}的方式表示

集合:无序,可由列表创建,其中元素不重复,用花括号{x,y,z}的方式表示

 

英文词频统计:

str='''If I should stay I would only be in your way

So I'll go But I know

I'll think of your every step of the way

And I will always love you Will always love you

You my darling you Bitter-sweet memories

That is all I'm taking with me

So goodbye Please don't cry We both know I'm not

What you need And I will always love you

I will always love you

I hope life treats you kind

And I hope you have all you dreamed of

And I wish you joy and happiness

But above all this Ii wish your love

And I will always love you

I will always love you I will always love you

I will always love you I will always love you

I will always love you Darling I love you

I'll always love you'''

#每个单词的第二个以后都变成小写
a=str.lower()
print(a)
#删除空格
str=str.lstrip()
print(str)
#分隔出一个一个的单词 list
print("将歌词分隔一个一个的单词为:")
strlist=str.split()
print(strlist)
#统计每个单词出现的次数 dict
print("统计每个单词出现的次数为:")
strset=set(strlist)
for word in strset:
    print(word,strlist.count(word))

 

posted @ 2018-10-08 10:48  陈松林  阅读(109)  评论(0)    收藏  举报