标签:break 键值 ems 分析字符串 app text ges [88 really
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
d=[‘11‘,‘22‘,‘33‘,‘44‘,‘55‘] score=[88,75,66,98,53] grade=dict(zip(id,score)) grade[‘12‘]=90 print(‘增加一个学生:‘,grade) grade.pop(‘44‘) print(‘删除一个学生:‘,grade) grade[‘33‘]=59 print(‘修改一个学生:‘,grade) print(‘查找11学生分数:‘,grade[‘11‘]) print(‘查询字典里面没有的学生:‘,grade.get(‘15‘,‘找不到该学生‘))
2.列表,元组,字典,集合的遍历。
a=list(‘56145462355‘) print("列表:",a) for i in a: print(i) b=tuple(‘56145462355‘) print("元组:",b) for i in b: print(i) id={‘001‘,‘002‘,‘003‘} scores={69,72,85} c=dict(zip(id,scores)) print("字典:",c) for i in c: print(i,c[i]) d=set(‘56145462355‘) print("集合:",d) for i in d: print(i)
总结列表,元组,字典,集合的联系与区别。
列表 | 元组 | 集合 | 字典 | |
---|---|---|---|---|
英文 | list | tuple | set | dict |
可否读写 | 读写 | 只读 | 读写 | 读写 |
可否重复 | 是 | 是 | 否 | 是 |
存储方式 | 值 | 值 | 键(不能重复) | 键值对(键不能重复) |
是否有序 | 有序 | 有序 | 无序 | 无序,自动正序 |
初始化 | [1,‘a‘] |
(‘a‘, 1) |
set([1,2]) 或 {1,2} |
{‘a‘:1,‘b‘:2} |
添加 | append |
只读 | add |
d[‘key‘] = ‘value‘ |
读元素 | l[2:] |
t[0] |
无 | d[‘a‘] |
3.英文词频统计实例
1.待分析字符串
2.分解提取单词
1.大小写 txt.lower()
2.分隔符‘.,:;?!-_’
3.单词列表
3.单词计数字典
music=‘‘‘When I was young I‘d listen to the radio Waiting for my favorite songs When they played I‘d sing along, It make me smile. Those were such happy times and not so long ago How I wondered where they‘d gone. But they‘re back again just like a long lost friend All the songs I love so well. Every shalala every wo‘wo still shines. Every shing-a-ling-a-ling that they‘re starting to sing so fine When they get to the part where he‘s breaking her heart It can really make me cry just like before. It‘s yesterday once more. (Shoobie do lang lang) (Shoobie do lang lang) Looking bak on how it was in years gone by And the good times that had makes today seem rather sad, So much has changed.‘‘‘ music=music.lower() print(music) for i in ‘,.‘: music=music.replace(i,‘‘) words=music.split(‘ ‘) print(words) dic={} for i in words: dic[i]= music.count(i) music=list(dic.items()) print(‘单词计数字典:‘,music,‘\n‘)
标签:break 键值 ems 分析字符串 app text ges [88 really
原文地址:http://www.cnblogs.com/acef/p/7597230.html