标签:items 遍历 评分 out end 合数 span sort only
1、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
score=list(‘21223113321‘) print(‘作业评分列表:‘,score) score.append(‘3‘) print(‘增加:‘,score) score.pop() print(‘删除:‘,score) score.insert(2,‘1‘) print(‘插入:‘,score) score[2]=‘2‘ print(‘修改:‘,score) print(‘第一个3分的下标:‘,score.index(‘3‘)) print(‘1分的个数:‘,score.count(‘1‘)) print(‘3分的个数:‘,score.count(‘3‘))
2.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
>>> k={‘201406114326‘:‘3‘,‘201406114327‘:‘2‘,‘201406114328‘:‘1‘,‘201406114329‘:‘0‘} >>> k[‘201406114326‘] ‘3‘ >>> k.pop(‘201406114327‘) ‘2‘ >>> k {‘201406114326‘: ‘3‘, ‘201406114328‘: ‘1‘, ‘201406114329‘: ‘0‘} >>> k.keys() dict_keys([‘201406114326‘, ‘201406114328‘, ‘201406114329‘]) >>> k.values() dict_values([‘3‘, ‘1‘, ‘0‘]) >>> k.items() dict_items([(‘201406114326‘, ‘3‘), (‘201406114328‘, ‘1‘), (‘201406114329‘, ‘0‘)]) >>> k.get(‘201406114326‘) ‘3‘ >>> k.get(‘201406114327‘,‘无结果‘) ‘无结果‘ >>>
3.列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。
ls=list("abcdefghijklmnopqrstuvwxyz") tu=tuple("abcdefghijklmnopqrstuvwxyz") di={‘语文‘:1,"数学":2,"英语":3,"物理":4,"化学":5,"生物":6} se=set("123131321") for i in tu: print(i)
4.英文词频统计实例
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])
标签:items 遍历 评分 out end 合数 span sort only
原文地址:http://www.cnblogs.com/hzlhzl/p/7570238.html