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

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()

转载于:https://www.cnblogs.com/a122624/p/7506157.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值