第八次作业
2017-09-22 14:17 019王泽 阅读(166) 评论(0) 收藏 举报1.字典实例:建立学生学号成绩字典,做增删改查遍历操作
name=['001','002','003','004']
scores=[7,6,6,5]
d={'001':7,'002':6,'003':6,'004':5}
print(d)
d['005']=5
print(d)
d.pop('003')
print(d)
d['002']=5
print(d)
print(d.items())
2.列表,元组,字典,集合的遍历。
l= list('123131312323')
t = tuple('31123131131')
d = dict(zip([1,2,3,4],[4,3,2,1]))
s = set(l)
for i in l:
print(i)
for i in t:
print(i)
for i in s:
print(i)
for i in d:
print(i)
3.总结列表,元组,字典,集合的联系与区别
列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素,元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。字典类似于你通过联系人名称查找地址和联系人详细情况的地址簿.
4.英文词频统计实例
n='''The driver, being a sensitive man as well as an animal lover, pulled over to the side of the road, and got out to see what had become of the Bunny carrying the basket. Much to his dismay, the colorful Bunny was dead. The driver felt guilty and began to cry.A woman driving down the same highway saw the man crying on the side of the road and pulled over. She stepped out of her car and asked the man what was wrong."I feel terrible," he explained, "I accidentally hit the Easter Bunny and killed it. There may not be an Easter because of me. What should I do?"'''
n=n.lower()
n=n.replace('\n',' ')
word=n.split(' ')
dic={}
keys=set(word)
for i in keys:
dic[i]=word.count(i)
print(dic)
浙公网安备 33010602011771号