标签:ati 遍历 查找 字符串分解 ima out text word com
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
dic={} xuehao=[‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘] scores=[‘85‘,‘95‘,‘65‘,‘75‘,‘88‘,‘99‘,‘55‘] xs=dict(zip(xuehao,scores)) print(‘学号及成绩:‘,xs) xs[‘8‘]=‘83‘ print(‘增加一位同学学号为8成绩为83:‘,xs) xs.pop(‘7‘) print(‘删除7号同学的信息‘,xs) xs[‘1‘]=‘82‘ print(‘将1号同学的分数改为82分‘,xs) print(‘判断是否含6号同学‘,xs.get(‘6‘)) print(‘查询5号同学成绩‘,xs[‘5‘])
2.列表,元组,字典,集合的遍历。
ls=list(‘1256642543‘) print("列表:",ls) for i in ls: print(i) tp=tuple(‘1256642543‘) print("元组:",tp) for a in tp: print(a) dic={} names=[‘小明‘,‘小莉‘,‘小红‘,‘小王‘] scores=[‘88‘,‘99‘,‘86‘,‘83‘] ns=dict(zip(names,scores)) print("字典:",ns) for b in ns: print(b,ns[b]) n=set(names) print("集合:",n) for c in n: print(c,end=‘ ‘)
总结列表,元组,字典,集合的联系与区别。
①列表,元组是有顺序的,而字典和集合是没顺序的。
②列表是可变对象,可以有增删改操作,而元组是只读的,不能修改。
③字典使存储键值对数据,键是不可变的对象。插入和查找速度快,不会随着键的增加而变慢,需要占用大量的内存。
④字典是用空间换取时间的一种方法。集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。
英文词频统计实例
s=‘‘‘In the romantic movie New York, I Love You, which was filmed by several short stories. There was a story about an old couple.They walked to the beach, while on the way, the woman complained about the man on many small issues, but the men barely argued. Finally, when they reached the destination, they suddenly hand in hand and appreciated the sunset. What a lovely story, it is so close to the real life, just like every couple’s final end. ‘‘‘ 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)
标签:ati 遍历 查找 字符串分解 ima out text word com
原文地址:http://www.cnblogs.com/33333-/p/7595992.html