组合数据类型练习,英文词频统计实例上

1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。

num={'01':'75','02':'87','03':'89','04':'91','05':'69'}
num['03']
print(num)
num['06']='70'
print(num)
num['05']='87'
print(num)
del(num['01'])
print(num)

2.列表,元组,字典,集合的遍历。

ab=list('123321213') #列表
ba=tuple('123321213')#元祖
dic={} #字典
names=['ab','bc','cd']
scores=['77','81','99']
za=dict(zip(names,scores))
s=set(names) #集合
for i in ab:
print(i,end=' ')
for a in ba:
print(a)
for b in za:
print(b,za[b])
for c in s:
print(c,end=' ')

总结列表,元组,字典,集合的联系与区别。

(1)列表是任意对象的序列。列表用方括号表示。

(2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。

(3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。

(4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。字典是Python解释器中最完善的数据类型。

3.英文词频统计实例

1.待分析字符串分解提取单词  2.大小写 txt.lower()分隔符'.,:;?!-_’   3.单词列表

s='''Hello, it's me
I was wondering if after all these years
You'd like to meet, to go over
Everything
They say that time's supposed to heal ya
But I ain't done much healing
 
Hello, can you hear me?
I'm in California dreaming about who we used to be
When we were younger and free
I've forgotten how it felt before the world fell at our feet
 
There's such a difference between us
And a million miles
 
Hello,from the other side
I must've called a thousand times to tell you
I'm sorry, for everything that I've done
But when I call you never seem to be home
 
Hello,from the outside
At least I can say that I've tried to tell you
I'm sorry, for breaking your heart
But it don't matter, it clearly doesn't tear you apart anymore
 
Hello, how are you?
It's so typical of me to talk about myself
I'm sorry, I hope that you're well
Did you ever make it out of that town
Where nothing ever happened?'''
s=s.lower()     
for i in ',.?':
    s=s.replace(i,' ')    
ja=s.split(' ')       
di = {}
ja.sort()
disc = set(ja)
for i in disc:
    di[i] = 0
for i in ja:
    di[i] = di[i]+1
items = di.items()
print(items)

 

posted @ 2017-09-26 13:05  JaTae  阅读(179)  评论(0编辑  收藏  举报