组合数据类型练习,英文词频统计实例上
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
>>> d={2015001:"75分", 2015002:"95分",2015003:"85分"} >>> d[201504]="100分" >>> print("增加后:",d) 增加后: {2015001: '75分', 2015002: '95分', 2015003: '85分', 201504: '100分'} >>> d.pop(2015001) '75分' >>> print("删除后:",d) 删除后: {2015002: '95分', 2015003: '85分', 201504: '100分'} >>> d[201502]="90分" >>> print("修改后:",d) 修改后: {2015002: '95分', 2015003: '85分', 201504: '100分', 201502: '90分'} >>> print("查找学号2015005:",d.get(2015005)) 查找学号2015005: None >>> print("遍历后:", d.items()) 遍历后: dict_items([(2015002, '95分'), (2015003, '85分'), (201504, '100分'), (201502, '90分')])
2.列表,元组,字典,集合的遍历。
>>> ls=list('2323121') >>> ls ['2', '3', '2', '3', '1', '2', '1'] >>> for i in ls: print(i) 2 3 2 3 1 2 1 >>> tu=tuple('2323121') >>> tu ('2', '3', '2', '3', '1', '2', '1') >>> for i in tu: print(i) 2 3 2 3 1 2 1 >>> d={"a":0,"b":2,"c":3} >>> d {'a': 0, 'b': 2, 'c': 3} >>> for i in d: print(i,d[i]) a 0 b 2 c 3 >>> se=set('2323121') >>> se {'3', '2', '1'} >>> for i in se: print(i) 3 2 1
总结列表,元组,字典,集合的联系与区别。
3.英文词频统计实例
1.待分析字符串
2.分解提取单词
1.大小写 txt.lower()
2.分隔符'.,:;?!-_’
3.单词列表
3.单词计数字典
s='''Met you by surprise I didn't realize That my life would change forever Saw you standing there I didn't know I cared,There was something special in the air Dreams are my reality The only kind of real fantasy, illusions are a common thing I try to live in dreams It seems as it's meant to be Dreams are my reality! A different kind of reality I dream of loving in the night And loving seems alright Although it's only fantasy If you do exist honey don't resist Show me a new way of loving Tell me that it's true show me what to do I feel something special about you Dreams are my reality The only kind of reality May be my foolishness is past And may be now at last I'll see how the real thing can be Dreams are my reality! ''' s=s.lower() for i in ',!': s=s.replace(i,' ') s=s.split(' ') print(s) dict={} for i in s: dict[i]=s.count(i) print(dict)
截图: