标签:计数 集合 大小 lin 下标 like 类型 div blog
1.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
s=list(‘kkkkk‘) s.insert(3,2) s.pop(3) s[6]=1 print(s) print(‘第一个1的位置是:‘,s.index(‘1‘)) x=0 y=0 for i in range(len(s)): if s[i]==‘3‘: x=x+1 if x==1: y=i print(‘三分个数为:‘,x,‘ ‘,‘第一个三分位置是:‘,y) print(‘二分个数为:‘,s.count(‘2‘))
2.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
s={‘小明‘:‘1‘,‘小红‘:‘3‘,‘小华‘:‘2‘} print(‘查询小明的值:‘,s.get(‘小明‘)) s.pop(‘小华‘) s[‘小李‘]=‘5‘ print(‘移除小华和增加小李:‘,s) s[‘小李‘]=‘6‘ print(‘修改小李:‘,s)
3.列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。
s=list(‘456132798‘) t=set(‘4561327489‘) c={‘小明‘:‘1‘,‘小红‘:‘3‘,‘小华‘:‘2‘} x=(1, 2, 3, 4, 5 ) print(‘列表的遍历为:‘,end=‘‘) for i in s: print(i,end=‘‘) print() print(‘元祖的遍历为:‘,end=‘‘) for i in x: print(i,end=‘‘) print() print(‘字典key的遍历为:‘,end=‘‘) for i in c: print(i,end=‘‘) print() print(‘字典value的遍历为:‘,end=‘‘) for i in c.values(): print(i,end=‘‘) print() print(‘数组的遍历为:‘,end=‘‘) for i in x: print(i,end=‘‘)
4.
英文词频统计实例
A、待分析字符串
B、分解提取单词
a、大小写 txt.lower()
b、分隔符‘.,:;?!-_’
C、计数字典
D、排序list.sort()
E、输出TOP(10)
s=‘‘‘Well I wonder could it be When I was dreaming about you baby You were dreaming of me Call me crazy Call me blind To still be suffering is stupid after all of this time Did I lose my love to someone better And does she love you like I do I do, you know I really really do Well hey So much I need to say Been lonely since the day The day you went away So sad but true For me there‘s only you Been crying since the day the day you went away.!?‘‘‘ print("do出现次数",s.count(‘do‘)) s=s.replace(",","") s=s.replace(‘.‘,‘‘) s=s.replace(‘?‘,‘‘) s=s.replace(‘!‘,‘‘) s=s.replace(‘\n‘,‘‘) s=s.lower() s1=s.split(‘ ‘) key=set(s1) dic={} for i in key: dic[i]=s.count(i) wc=list(dic.items()) wc.sort(key=lambda x:x[1],reverse=True) for i in range(10): print(wc[i])
标签:计数 集合 大小 lin 下标 like 类型 div blog
原文地址:http://www.cnblogs.com/lingg/p/7565333.html