作业3

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

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

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

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

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

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)

 

posted @ 2018-10-08 11:43  曾霖  阅读(136)  评论(0编辑  收藏  举报