码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-22 18:21:33      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:body   about   必须   获取   got   tor   print   分享   get   

练习

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

  • 列表,元组,字典是有顺序的,而集合是没顺序的
  • 列表是以方括号形式表示,元组是以圆括号表示,字典以花括号表示,集合则是以[()]的形式表示
  • 列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。区别于元组,可动态增加,删除,更新。
  • 元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。元组一旦定义其长度和内容都是固定的。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。若想创建包含一个元素的元组,则必须在该元素后面加逗号“,”,否则创建的不是一个元组,而是一个字符串。
  • 集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。
  • 字典最大的价值是查询,通过键,查找值。

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

#coding=utf-8
# anthor: Vitan

# 列表的遍历
list=[‘Google‘,‘Android‘,123]
for li in list:
    print(li)
print(‘===============‘)
# 元组的遍历
tu=(‘Google‘,‘Android‘,123)
for t in tu:
    print(t)
print(‘===============‘)
# 字典遍历
dict={‘Google‘:‘Tech‘,‘Tesla‘:‘Car‘,‘OnePlus‘:‘SamrtPhone‘}
for dic in dict:
    print(dic)
print(‘===============‘)
# 集合的遍历
set = set([‘Google‘,‘Facebook‘,‘Tesla‘,‘OnePlus‘])
for par in set:
    print(par)

技术分享图片

英文词频统计

  • 要求
    • 下载一首英文的歌词或文章str
    • 分隔出一个一个的单词 list
    • 统计每个单词出现的次数 dict
str=‘‘‘There‘s a girl but I let her get away
It‘s all my fault cause pride got in the way
And I‘d be lying if I said I was ok
About that girl the one I let get away
I keep saying no
This can‘t be the way we‘re supposed to be
I keep saying no
There‘s gotta be a way to get you close to me
Now I know you gotta
Speak up if you want somebody
Can‘t let him get away, oh no
You don‘t wanna end up sorry
The way that I‘m feeling everyday
No no no no
There‘s no hope for the broken heart
No no no no
There‘s no hope for the broken
There‘s a girl but I let her get away
It‘s my fault cause I said I needed space
I‘ve been torturing myself night and day
About that girl, the one I let get away
I keep saying no
This can‘t be the way we‘re supposed to be
I keep saying no
There‘s gotta be a way to get you
There‘s gotta be a way
To get you close to me
You gotta
Speak up if you want somebody
Can‘t let him get away, oh no
You don‘t wanna end up sorry
The way that I‘m feeling everyday
No no no no
There‘s no hope for the broken heart
No no no no
There‘s no hope for the broken
No home for me
No home cause I‘m broken
No room to breathe
And I got no one to blame
No home for me
No home cause I‘m broken
About that girl
The one I let get away
So you better speak up if you want somebody
Can‘t let him get away oh no no
You don‘t wanna end up sorry
The way that I‘m feeling everyday
Don‘t you know
No no no no
There‘s no hope for the broken heart
Don‘t you know
No no no no
There‘s no hope for the broken
You don‘t wanna lose at love
It‘s only gonna hurt too much
I‘m telling you
You don‘t wanna lose at love
It‘s only gonna hurt too much
You don‘t wanna lose at love
Cause there‘s no hope for the broken heart
About that girl
The one I let get away‘‘‘

str = str.lower() # 将字符串转为小写
str_list = str.split() # 将字符串行切片分割成单词列表
str_set=set(str_list) # 将列表转成集合,去除重复项
result_dict={}
for item in str_list:
    result_dict[item] = str_list.count(item)
print(result_dict,len(result_dict))

技术分享图片

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

标签:body   about   必须   获取   got   tor   print   分享   get   

原文地址:https://www.cnblogs.com/vitan/p/9690411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!