组合数据类型练习、英语词频统计

1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
dict={'02':59,"03":70,"04":96,"05":85} dict print(dict.get('04')) dict.pop('03') print(dict.items()) dict['01']=60 print(dict.items()) dict["06"]=74 print(dict.items()) print("查找:") print(dict.get('05','不存在')) print(dict.get('09','不存在')) print(dict.items()) print("遍历") for i in dict: print(i)

 

2.列表,元组,字典,集合的遍历。
lis=["10","09","08","07"] tu=("10","09","08","07","06") dic={"10":"63","09":"75","08":"66","07":"92"} s=set(lis) for i in lis: print(i) for i in tu: print(i) for i in dic: print('{0:<}:{1:>}'.format(i,dic[i])) for i in s: print(i)


总结列表,元组,字典,集合的联系与区别。

①列表,元组是有顺序的,而字典和集合是没顺序的。

②列表是可变对象,可以有增删改操作,而元组是只读的,不能修改。

③字典使存储键值对数据,键是不可变的对象。插入和查找速度快,不会随着键的增加而变慢,需要占用大量的内存。

④字典是用空间换取时间的一种方法。集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。

 

英文词频统计实例
  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典

s='''4... 3... 2... 1... no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire even if you say I have been the world wide I'll take you where surely you have never been all right in the fight I'm OK... come on come on hey do you feel the night is breathable look at this town which is unbelievable no other places like that in the world world world 1 2 3 4 no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire turning to the left easy chicks and red lights and to the right crazy music everywhere all right in the fight I'm OK... come on come on hey do you feel the night is breathable look at this town which is unbelievable no other places like that in the world world world 1 2 3 4 no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire come on 1 2 3 4 all right crossing the line Tokyo is on fire hey do you feel the night is breathable look at this town which is unbelievable no other places like that in the world world world 1 2 3 4 no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire ''' print('短文中各个短语出现的次数:') s=s.lower() print('全部转为小写:') print(s) for i in ',.': s=s.replace(i,' ') words=s.split(' ') d = {} words.sort() disc = set(words) for i in disc: d[i] = 0 for i in words: d[i] = d[i]+1 n = d.items() print('单词计数:') print(n)

 

posted on 2017-09-26 20:00  122叶远超  阅读(143)  评论(0编辑  收藏  举报

导航