1.练习:
总结列表,元组,字典,集合的联系与区别。
列表是可变的对象,可进行动态的增加、删除、更新,用[]表示。
元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。
字典是存储键值对数据的对象,字典的元素都是无序的,且键不能相同,可以通过键,找到值,字典最外面用大括号,每一组用冒号连接起来。
集合相当于字典的键,也是一个无序不重复的元素集,用一对{}表示。
列表,元组,字典,集合的遍历。
列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。字典和集合 两者都是无序的,数据量大时可用集合或列表来创建。
a = list('hello world!') #列表的遍历
print(a)
for i in a:
print(i)
b = tuple('344578655') #元组的遍历
print(b)
for i in b:
print(i)
c = set('lkfsfdr') #集合的遍历
print(c)
for i in c:
print(i)
d = {'A':45,'B':99,'D':68} #字典的遍历
print(d)
for i in d:
print(i,d[i])
2.英文词频统计:
- 下载一首英文的歌词或文章str
- 分隔出一个一个的单词 list
- 统计每个单词出现的次数 dict
musicstr = '''I'v been brokehearted
ever since the day we partied
I didn't know where to turn to
ever since you went away
I goin' in circles
wander if really hurts you
I know i don't deserve it
But I'm dealing with it everyday
I was so surprised
I didn't know what to say
But i forgave you
for throwing all away
I've been hopin' you come back to me
I forgive you..... for everything
I've been crying about everything
Now I won't be satisfied till you come back to me
It really don't make sence
i've been trying to figure out why you left
I've picking up the pieces
now I wish you're here with me
I'v been brokehearted
ever since the day we partied
I don't have much to say
Haven't seen you since you've gone away
I've been hopin' you come back to me
I forgive you for everything
I've been crying about everything
Now I won't be satisfied till you come back to me
wanna be stay
come back to me
Cause I know I don't wanna be without you, baby
I've been hopin' you come back to me
I forgive you for everything
I've been crying about everything
Now I won't be satisfied till you come back to me
wanna be stay
come back to me
I won't be satisfied till you come back to me'''
musicstr = musicstr.lower()
sep = ''',.:!?'"'''
for c in sep:
musicstr = musicstr.replace(c,' ')
musicList= musicstr.split()
print(musicstr)
print(musicList)
dd=set(musicList)
print(dd)
for word in dd:
print(musicList.count(word))