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

1、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等

score=list('3212233331133213')
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、字典实例:建立学生学号成绩字典,做增删改查遍历操作。

d={'牛三':95,'牛一':78,'牛二':65,'牛牛':66}
print('学生成绩字典:',d)
d['牛五']=98
print('增加:',d)
d.pop('牛牛')
print('删除:',d)
d['牛三']=78
print('修改:',d)
print('查询牛一成绩:',d.get('牛一',''))

3、列表,元组,字典,集合的遍历。

s=list('3856132798')
t=set('2581327489')
c={'牛一':'1','牛二':'3','牛三':'2'}
x=(1, 2, 3, 4, 5 )
print('列表的遍历为:',end='')
for i in s:
print(i,end='')
print()
print('元祖的遍历为:',end='')
for i in x:
print(i,end='')
print()
print('字典key的遍历为:',end='')
for i in c:
print(i,end='')
print()
print('字典value的遍历为:',end='')
for i in c.values():
print(i,end='')
print()
print('数组的遍历为:',end='')
for i in x:
print(i,end='')

 

4、英文词频统计实例

s = '''we wish you a merry christmas 
we wish you a merry christmas 
we wish you a merry christmas 
and a happy new year 
good tidings we bring 
to you and your kin 
good tidings for christmas 
and a happy new year 
we wish you a merry christmas 
we wish you a merry christmas 
we wish you a merry christmas 
and a happy new year 
and have yourself a merry little christmas, 
have yourself a merry little christmas night 
Jingle Bells 
(The One Horse Open Sleigh) 
Lyrics:James Lord Pierpont Music:James Lord Pierpont 

Dashing through the snow, 
In a one horse open sleigh, 
Over the fields we go, 
Laughing all the way; 
Bells on bobtails ring, 
Making spiritis bright, 
What fun it is to ride and sing a sleighing song to night. 

Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 
Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 

A day or two ago I thought I'd take a ride, 
And soon Miss Fannie Bright, 
Was seated by my side; 
THe horse was lean and lank, 
Misfortune seemed his lot, 
He got into a drifted bank, 
And we, we got upsot. 

Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 
Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 

Now the ground is white, 
Go it while you're young, 
Take the girls tonight, 
And sing this sleighing song. 
Just get a bobtailed bay, 
Two-forty for this spead, Then hitch him to an open sleigh, 
And crack! You'll take the lead. 

Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh! 
Jingle Bells! Jingle Bells! Jingle all the way! 
Oh, what fun it is to ride in a one horse open sleigh!'''
 
s = s.lower()
for i in '?!,.\'\n':
    s = s.replace(i,' ')
words = s.split(' ')
 
dic={}
keys = set(words)
for i in keys:
    dic[i]=words.count(i)
 
c = list(dic.items())
c.sort(key=lambda x:x[1],reverse=True)
 
for i in range(10):
    print(c[i])
('', 112)
('and', 15)
('you', 14)
('the', 13)
('they', 10)
('to', 9)
('starry', 8)
('how', 8)
('in', 8)
('not', 7)

 

 

posted @ 2017-09-21 11:44  少钏_leo  阅读(181)  评论(0编辑  收藏  举报