组合数据类型练习,英文词频统计实例上
2017-09-26 13:32 055李小锐 阅读(186) 评论(0) 收藏 举报1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
>>> d={} #增 >>> d['201508030055']='100' >>> print(d) {'201508030055': '100'} #判断 >>> 201508030044 in d False #增 >>> d['201508030054']='50' >>> d['201508030053']='80' >>> print(d) {'201508030055': '100', '201508030054': '50', '201508030053': '80'} #删 >>> d.pop('201508030054') '50' >>> d {'201508030055': '100', '201508030053': '80'} #改 >>> d['201508030055']='90' >>> d {'201508030055': '90', '201508030053': '80'} #查 >>> d.get('201508030053') '80' >>> print(d.get('201508030053')) 80 >>> print(d.get('2015080300555')) None
2.列表,元组,字典,集合的遍历。
#列表遍历 s1=list('211211344') for i in s1: print(i) >>> RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/h.py 2 1 1 2 1 1 3 4 4 #元组遍历 s2=tuple('211211344') s2 for i in s2: print(i) >>> RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/h.py 2 1 1 2 1 1 3 4 4 2 1 1 2 1 1 3 4 4 #字典遍历 s3={} s3['aaa']='100' s3['bbb']='80' s3['ccc']='100' s3['ddd']='100' for i in s3: print(i,s3[i]) >>> RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/h.py aaa 100 bbb 80 ccc 100 ddd 100 #集合遍历 s4=set('11233322') s4 for i in s4: print(i) >>> RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/h.py 3 1 2
总结列表,元组,字典,集合的联系与区别。
(1)列表是任意对象的序列,列表用方括号表示。 如:['2', '1', '3', '4', '3', '2', '1'] (2)元组是将一组值打包到一个对象中,用圆括号表示。可以随时插入,删除;而元组一旦确认就不能够再更改。元组在定义过程中,字符串必须用单引号‘扩起来。 如:('2', '1', '3', '4', '3', '2', '1') (3)字典就是一个关联数组,由键和值组成,其中包含通过键的对象。用大括号表示。与集合相比,比集合访问方便。 如:{'a': '100', 'b': '10'} (4)与列表和元组不同,集合是无序的,也不能通过索引进行访问。而且集合中的元素不能重复。 如:{'2', '3', '1'}
3.英文词频统计实例
待分析字符串分解提取单词
大小写 txt.lower()
分隔符'.,:;?!-_’
单词列表
s='''It's been a long day without you my friend
And I'll tell you all about it when I see you again We've come a long way from where we began Oh I'll tell you all about it when I see you again When I see you again Damn who knew all the planes we flew Good things we've been through That I'll be standing right here Talking to you about another path I know we loved to hit the road and laugh But something told me that it wouldn't last Had to switch up look at things different see the bigger picture Those were the days hard work forever pays Now I see you in a better place How could we not talk about family when family's all that we got? Everything I went through you were standing there by my side And now you gonna be with me for the last ride It's been a long day without you my friend And I'll tell you all about it when I see you again We've come a long way from where we began Oh I'll tell you all about it when I see you again When I see you again First you both go out your way And the vibe is feeling strong and what's Small turn to a friendship a friendship Turn into a bond and that bond will never Be broke and the love will never get lost And when brotherhood come first then the line Will never be crossed established it on our own When that line had to be drawn and that line is what We reach so remember me when I'm gone How could we not talk about family when family's all that we got? Everything I went through you were standing there by my side And now you gonna be with me for the last ride Let the light guide your way hold every memory As you go and every road you take will always lead you home Hoo It's been a long day without you my friend And I'll tell you all about it when I see you again We've come a long way from where we began Oh I'll tell you all about it when I see you again When I see you again Again When I see you again see you again When I see you again'''
#将单词变成小写 s=s.lower()
#替换分隔符
for i in ',.?':
s=s.replace(i,'')
#逐个分隔单词
words=s.split(" ") word=set(words) print(words)
>>> == RESTART: C:/Users/lenovo/AppData/Local/Programs/Python/Python36-32/aa.py == ["it's", 'been', 'a', 'long', 'day', 'without', 'you', 'my', 'friend\n\nand', "i'll", 'tell', 'you', 'all', 'about', 'it', 'when', 'i', 'see', 'you', "again\n\nwe've", 'come', 'a', 'long', 'way', 'from', 'where', 'we', 'began\noh', "i'll", 'tell', 'you', 'all', 'about', 'it', 'when', 'i', 'see', 'you', 'again\nwhen', 'i', 'see', 'you', 'again\n\ndamn', 'who', 'knew', 'all', 'the', 'planes', 'we', 'flew\ngood', 'things', "we've", 'been', 'through\nthat', "i'll", 'be', 'standing', 'right', 'here\ntalking', 'to', 'you', 'about', 'another', 'path\ni', 'know', 'we', 'loved', 'to', 'hit', 'the', 'road', 'and', 'laugh\nbut', 'something', 'told', 'me', 'that', 'it', "wouldn't", 'last\nhad', 'to', 'switch', 'up', 'look', 'at', 'things', 'different', 'see', 'the', 'bigger', 'picture\nthose', 'were', 'the', 'days', 'hard', 'work', 'forever', 'pays\nnow', 'i', 'see', 'you', 'in', 'a', 'better', 'place\n\nhow', 'could', 'we', 'not', 'talk', 'about', 'family', 'when', "family's", 'all', 'that', 'we', 'got\neverything', 'i', 'went', 'through', 'you', 'were', 'standing', 'there', 'by', 'my', 'side\nand', 'now', 'you', 'gonna', 'be', 'with', 'me', 'for', 'the', 'last', "ride\nit's", 'been', 'a', 'long', 'day', 'without', 'you', 'my', 'friend\n\nand', "i'll", 'tell', 'you', 'all', 'about', 'it', 'when', 'i', 'see', 'you', "again\n\nwe've", 'come', 'a', 'long', 'way', 'from', 'where', 'we', 'began\noh', "i'll", 'tell', 'you', 'all', 'about', 'it', 'when', 'i', 'see', 'you', 'again\n\nwhen', 'i', 'see', 'you', 'again\n\nfirst', 'you', 'both', 'go', 'out', 'your', 'way\nand', 'the', 'vibe', 'is', 'feeling', 'strong', 'and', "what's\nsmall", 'turn', 'to', 'a', 'friendship', 'a', 'friendship\nturn', 'into', 'a', 'bond', 'and', 'that', 'bond', 'will', 'never\nbe', 'broke', 'and', 'the', 'love', 'will', 'never', 'get', 'lost\n\nand', 'when', 'brotherhood', 'come', 'first', 'then', 'the', 'line\nwill', 'never', 'be', 'crossed', 'established', 'it', 'on', 'our', 'own\nwhen', 'that', 'line', 'had', 'to', 'be', 'drawn', 'and', 'that', 'line', 'is', 'what\nwe', 'reach', 'so', 'remember', 'me', 'when', "i'm", 'gone\n\nhow', 'could', 'we', 'not', 'talk', 'about', 'family', 'when', "family's", 'all', 'that', 'we', 'got\neverything', 'i', 'went', 'through', 'you', 'were', 'standing', 'there', 'by', 'my', 'side\nand', 'now', 'you', 'gonna', 'be', 'with', 'me', 'for', 'the', 'last', 'ride\nlet', 'the', 'light', 'guide', 'your', 'way', 'hold', 'every', 'memory\nas', 'you', 'go', 'and', 'every', 'road', 'you', 'take', 'will', 'always', 'lead', 'you', "home\n\nhoo\n\nit's", 'been', 'a', 'long', 'day', 'without', 'you', 'my', 'friend\n\nand', "i'll", 'tell', 'you', 'all', 'about', 'it', 'when', 'i', 'see', 'you', "again\n\nwe've", 'come', 'a', 'long', 'way', 'from', 'where', 'we', 'began\noh', "i'll", 'tell', 'you', 'all', 'about', 'it', 'when', 'i', 'see', 'you', 'again\nwhen', 'i', 'see', 'you', 'again\nagain\n\nwhen', 'i', 'see', 'you', 'again', 'see', 'you', 'again\n\nwhen', 'i', 'see', 'you', 'again']
单词计数字典
#创建词的字典 dic={} for i in word: dic[i]= words.count(i) #输出以词频作为数组的列表 words=list(dic.items()) words.sort(key=lambda x:x[1],reverse=True) print(words,'\n') #遍历词频前十的数组 for i in range(10): word,count=words[i] print("{} {}".format(word,count))
[('you', 31), ('see', 14), ('i', 14), ('the', 10), ('when', 10), ('a', 10), ('all', 9), ('we', 9), ('about', 9), ('it', 8), ("i'll", 7), ('tell', 6), ('that', 6), ('and', 6), ('long', 6), ('be', 5), ('to', 5), ('my', 5), ('way', 4), ('come', 4), ('me', 4), ('been', 4), ('from', 3), ('without', 3), ('where', 3), ('were', 3), ('will', 3), ("again\n\nwe've", 3), ('day', 3), ('began\noh', 3), ('standing', 3), ('friend\n\nand', 3), ('there', 2), ('go', 2), ('gonna', 2), ('never', 2), ('could', 2), ('with', 2), ('again\nwhen', 2), ('road', 2), ('again\n\nwhen', 2), ('by', 2), ('talk', 2), ('went', 2), ('things', 2), ('family', 2), ('every', 2), ('not', 2), ('your', 2), ('bond', 2), ("family's", 2), ('line', 2), ('got\neverything', 2), ('last', 2), ('for', 2), ('side\nand', 2), ('again', 2), ('through', 2), ('is', 2), ('now', 2), ('loved', 1), ('on', 1), ('picture\nthose', 1), ('turn', 1), ('something', 1), ('bigger', 1), ('right', 1), ('broke', 1), ('knew', 1), ('friendship', 1), ('know', 1), ('lead', 1), ('remember', 1), ('better', 1), ('told', 1), ('never\nbe', 1), ('again\n\ndamn', 1), ('what\nwe', 1), ('work', 1), ('place\n\nhow', 1), ('always', 1), ('pays\nnow', 1), ('ride\nlet', 1), ('drawn', 1), ('planes', 1), ('flew\ngood', 1), ('first', 1), ("home\n\nhoo\n\nit's", 1), ('into', 1), ('in', 1), ('here\ntalking', 1), ('gone\n\nhow', 1), ('established', 1), ('last\nhad', 1), ('own\nwhen', 1), ('another', 1), ('way\nand', 1), ('so', 1), ('strong', 1), ('again\nagain\n\nwhen', 1), ('who', 1), ('at', 1), ('line\nwill', 1), ("it's", 1), ("ride\nit's", 1), ('path\ni', 1), ('switch', 1), ('friendship\nturn', 1), ('through\nthat', 1), ('again\n\nfirst', 1), ('lost\n\nand', 1), ('both', 1), ('feeling', 1), ('different', 1), ('light', 1), ("what's\nsmall", 1), ('love', 1), ('guide', 1), ('up', 1), ('memory\nas', 1), ('out', 1), ('vibe', 1), ('our', 1), ('forever', 1), ('then', 1), ('laugh\nbut', 1), ("i'm", 1), ('get', 1), ('hit', 1), ('take', 1), ('hold', 1), ('days', 1), ('had', 1), ('hard', 1), ('crossed', 1), ("we've", 1), ('brotherhood', 1), ('reach', 1), ('look', 1), ("wouldn't", 1)]
you 31 see 14 i 14 the 10 when 10 a 10 all 9 we 9 about 9 it 8
浙公网安备 33010602011771号