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

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

fen=list('21223113321')
print('作业评分列表:',fen)
fen.append('3')
print('增加:',fen)
fen.pop()
print('删除:',fen)
fen.insert(2,'2')
print('插入:',fen)
fen[2]='1'
print('修改:',fen)
print('第一个3分的下标:',fen.index('3'))
print('1分的个数:',fen.count('1'))
print('3分的个数:',fen.count('3'))

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

d={'张三':93,'李四':74,'王五':45,'刘六':66}
print('学生成绩字典:',d)
d['钱二']=92
print('增加:',d)
d.pop('刘六')
print('删除:',d)
d['张三']=73
print('修改:',d)
print('查询李四成绩:',d.get('李四',''))

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

l=list('1234562216')
t=tuple('124578214')
s=set('321245632')
d={'a':1,'b':2,'c':3,'d':4}
print('列表',l)
for i in l:
    print(i,end='\t')
print('\n')
print('元组',t)
for i in t:
    print(i,end='\t')
print('\n')
print('集合',s)
for i in s:
    print(i,end='\t')
print('\n')
print('字典',d)
for i in d:
    print(i,d[i])

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

列表  有序,有下标。  可增删改查

元组  有序,有下标  只可读

字符串  不可以修改,只可以更新

字典  无序的。通过key找值,可以嵌套很多层

集合  无序的

4.英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
  3. 计数字典
  4. 排序list.sort()
  5. 输出TOP(10)
news='''Spend all your time waiting for that second chance
for the break that will make it ok
there's always some reason to feel not good enough
and it's hard at the end of the day
i need some distraction or a beautiful release
memories seep from my veins
let me be empty or weightless and maybe
l'll find some peace tonight
in the arms of the angel far away from here
from this dark cold hotel room and the endlessness that you feel
you are pulled from the wreckage of your silent reverie
you are in the arms of the angel, may you find some comfort here
so tired of the straight line and everywhere you turn
there's vultures and thieves at your back
the storm keeps on twisting, you keep on building the lies
that make up for all that you lack
it don't make no difference, escape one last time
it's easier to believe
in this sweet madness oh this glorious sadness
that brings me to my knees
in the arms of the angel far away from here
from this dark, cold hotel room and the endlessness that you feel
you are pulled from the wreckage of your silent reverie
in the arms of the angel ,may you find some comfort here
in the arms of the angel, may you find some comfort here'''
news=news.lower()
for i in '.,:;?!-_':
    news=news.replace(i,' ')
exc={'the','in','you','or','on'}

words=news.split(' ')
wc={}
keys=set(words)
for j in exc:
    keys.remove(j)
for i in keys:
    wc[i]=news.count(i)
wc=list(wc.items())
wc.sort(key=lambda x:x[1],reverse=True)
print(wc)
for i in range(10):
    print(wc[i])

 

文件操作

fo = open('test.txt','w')
fo.write('''every night in my dreams 
i see you, i feel you,
that is how i know you go on 
far across the distance 
and spaces between us 
you have come to show you go on 
near, far, wherever you are 
i believe that the heart does go on 
once more you open the door 
and you're here in my heart 
and my heart will go on and on 
love can touch us one time 
and last for a lifetime 
and never let go till we're one 
love was when i loved you 
one true time i hold to 
in my life we'll always go on 
near, far, wherever you are 
i believe that the heart does go on 
once more you open the door 
and you're here in my heart 
and my heart will go on and on 
there is some love that will not go away 
you're here, there's nothing i fear,
and i know that my heart will go on 
we'll stay forever this way 
you are safe in my heart 
and my heart will go on and on''')
fo.close()
fr=open('test.txt','r')
fr.read()
fr.close()

posted on 2017-09-21 15:56  yezless  阅读(134)  评论(0编辑  收藏  举报