DSJ666

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

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

 1.

列表(list)

具有以下特点: 
1.可以用list()函数或者方括号[]创建,元素之间用逗号’,‘’分隔。 
2.列表的元素不需要具有相同的类型 
3.使用索引来访问元素 
4.可切片

元组

元组跟列表很像,只不过元组用小括号来实现(),这里要区分好和生成器的区别,两者都可用小括号来实现的

具有以下特点: 
1.可以用tuple()函数或者方括号()创建,元素之间用逗号’,‘’分隔。 
2.元组的元素不需要具有相同的类型 
3.使用索引来访问元素 
4.可切片 
5.元素的值一旦创建就不可修改!!!!!(这是区别与列表的一个特征)

字典(Dictionary)

字典是另一种可变容器模型,且可存储任意类型对象。

具有以下特点: 
1.元素由键(key)和值(value)组成 
2.可以用dict()函数或者方括号()创建,元素之间用逗号’,‘’分隔,键与值之间用冒号”:”隔开 
3.键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组 
4.使用键(key)来访问元素

集合(set)

具有以下特点: 
1.可以用set()函数或者方括号{}创建,元素之间用逗号”,”分隔。 
2.与字典相比少了键 
3.不可索引,不可切片 
4.不可以有重复元素

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

list=[1,2,3,4,'s','d','f','g'] # 列表
for li in list:
    print(li)
print('----------')
tuple=('我','是','一','个','元','组') # 元组
for tu in tuple:
    print(tu)
print('----------')
dict={'是':'1','一':'2','个':'4','字':'3','典':'5',} # 字典
for d in dict:
    print(d)
print('----------')
set = set(['d','f','b'])
for s in set:
    print(s)
print('----------')

  运行结果如下

二、英文词频统计:

  • 下载一首英文的歌词或文章str
  • 分隔出一个一个的单词 list
  • 统计每个单词出现的次数 dict
s='''I'm so glad you made time to see me
How's life? Tell me how's your family
I haven't seen them in a while
You've been good, busier than ever
Small talk, work and the weather
Your guard is up and I know why
Because the last time you saw me
is still burned in the back of your mind
You gave me roses and I left them there to die
So this is me swallowing my pride standing in
front of you saying I'm sorry for that night
And I go back to December all the time
It turns out freedom ain't nothin but missin you
Wishin I'd realized what I had when you were mine
I go back to December turn around and make it all right
I go back to December all the time
These days I haven't been sleepin
Stayin up playing back myself leavin
When your birthday passed and I didn't call
Then I think about summer all the beautiful times
I watched you laughin from the passenger side
And realized I loved you in the fall
And then the cold came the dark days
when fear crept into my mind
You gave me all your lovin all I gave you was goodbye
So this is me swallowing my pride standing in
front of you saying I'm sorry for that night
And I go back to December all the time
It turns out freedom ain't nothin but missin you
Wishin I'd realized what I had when you were mine
I go back to December turn around and change my own mind
I go back to December all the time
I miss your tan skin, your sweet smile
So good to me so right
And how you held me in your arms that September night
The first time you ever saw me cry
Maybe this is wishful thinkin
Probably mindless dreamin
If we loved again I swear I'd love you right
I'd go back in time and change it but I can't
So if the chain is on your door I understand
This is me swallowing my pride standing in
front of you saying I'm sorry for that night
And I go back to December
It turns out freedom ain't nothin but missin you
Wishin I'd realized what I had when you were mine
I go back to December turn around and make it all right
I go back to December turn around and change my own mind
I go back to December all the time
all the time''' #歌词
s=s.lower()  #转为小写
c=''',.:?!'''
for b in c:
    s=s.replace(b,' ')  #去除符号
s=s.lstrip()  #去掉空格
  #分隔
strList=s.split()
print(len(strList),strList)
strSet = set(strList)
for word in strSet:
    print(word,strList.count(word))

  运行结果如下

 

posted on 2018-10-11 16:10  DSJ666  阅读(206)  评论(0)    收藏  举报

导航