第三次作业
1、
列表:用list()函数或者[]创建,是可变序列。
元组:用()或者tuple()函数来实现;是不可变序列。
字典:用dict()函数或者{}创建,使用键-值(key-value)存储,键是不可变的对象。
集合:用set()函数或者{}创建,是一个无序不重复元素集。
a = list('hello world!') #列表的遍历 print(a) for i in a: print(i) b = tuple('2379123') #元组的遍历 print(b) for i in b: print(i) c = set('abshdu') #集合的遍历 print(c) for i in c: print(i) d = {'Amy':79,'Dora':100,'Rose':90} #字典的遍历 print(d) for i in d: print(i,d[i])
2、
英文词频统计:
- 下载一首英文的歌词或文章str
- 分隔出一个一个的单词 list
- 统计每个单词出现的次数 dict
lyric='''I lay awake at night in my bed All these thoughts in my head about you Turn on the TV to drown out the sound of my herat cause it's bounding for you Try not to think of the smile I haven't seen ina while What am I gonna do My heart is beating so loud and I can't block it out Cause it's telling the truth That I miss your touch and I miss your kiss I never thought I would feel like this Oh,I miss your body next to mine Can't get this heartbeat out of mind There goes my heartbeat again,like a durm in my head just when I think about the things that you did and you said Can't stop this beating,baby,because it drives me crazy Can't stop the rhythm of this heartbeat,heartbeat I know that I'm not supposed to be feeling the way that I'm feeling inside I carry on with my day, but nothing goes my way, trying to understand why This thumping in my frame is driving me insane - why did I let you go? This rhyth's never ending, my heart isn't mending ''' lyric = lyric.replace('.',' ') lyList = lyric.split() print(len(lyList),lyList) lySet = set(lyList) print(lySet) lyDict={} for word in lySet: lyDict[word] = lyList.count(word) print(len(lyDict),lyDict)