首页 > 其他 > 详细

作业3

时间:2018-10-08 12:00:15      阅读:179      评论:0      收藏:0      [点我收藏+]

总结列表,元组,字典,集合区别

      列表是可变的对象,可进行动态的增加、删除、更新,用[]表示。

      元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改元组用“()”表示。

      字典是存储键值对数据的对象,字典的元素都是无序的,且键不能相同,可以通过键,找到值,字典最外面用大括号,每一组用冒号连接起来。

      集合相当于字典的键,也是一个无序不重复的元素集,用一对{}表示。

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

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

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

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

d = {v: 44, c: 233, q: 67}  # 字典的遍历
print(d)
for i in d:
    print(i, d[i])

技术分享图片

 

2.英文词频统计:

  • 下载一首英文的歌词或文章str
  • 分隔出一个一个的单词 list
  • 统计每个单词出现的次数 dict
noStr = ‘‘‘Beautiful girls all over the world.
I could be chasing but my time will be wasted.
They got nothin' on you baby.
Nothin' on you baby.
They might say hi and I might say hey.
But you shouldn't worry about what they say.
Coz they got nothin' on you baby.
Nothin' on you baby.
I know you feel where I'm coming from.
So baby regardless of the things in my past everything I've done.
Cause you and I know it was just for fun,
I was on the carrousel I just spun around.
With no direction, no intentions
It all ended.
It's so much nonsense,
it's on my conscience
I'm thinking "maybe I should get it out"
And I don't wanna sound redundant
But I was wondering,
if there was something that you wanna know
But never mind that,
we should let it go
Coz we don't wanna be a T.V. episode
And all the bad thoughts,
just let em go, go, go
Beautiful girls all over the world.
I could be chasing but my time will be wasted.
They got nothin' on you baby.
Nothin' on you baby.
They might say hi and I might say hey.
But you shouldn't worry about what they say.
Coz they got nothin' on you baby.
Nothin' on you baby.
I know you feel where I'm coming from.
So baby regardless of the things in my past everything I've done.
Cause you and I know it was just for fun,
I was on the carrousel I just spun around.
With no direction, no intentions.
It all ended.
It's so much nonsense,
it's on my conscience
I'm thinking "maybe I should get it out"
And I don't wanna sound redundant
But I was wondering,‘‘‘


noStr = noStr.lower()#大小写转换
for i in ‘‘‘,.!?‘‘‘:
    noStr=noStr.replace(i, )#空格代替符号
noStr1 = noStr.split()#出去两边空格
result = {}
for word in set(noStr1):
    result[word] = noStr1.count(word)
print(result)

技术分享图片

 

作业3

原文:https://www.cnblogs.com/Zengl/p/9753471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!