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

本文通过Python实例展示了字典、列表、元组、集合的使用与遍历,对比了它们之间的联系与区别,并提供了学生学号成绩字典的操作案例。此外,还详细介绍了如何利用Python进行英文文本的词频统计,包括文本预处理、单词提取与计数等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

d=['1','2','3','4','5']
s=[87,91,78,90,82]
g=dict(zip(d,s))
g['12']=90  
print('增加一个学生:',g)  
g.pop('3')    
print('删除一个学生:',g)  
g['4']=70
print('修改一个学生:',g) 
print('查找2学生分数:',g['2'])
print('查询字典里面没有的学生:',g.get('7','找不到该学生'))

 

2、列表,元组,字典,集合的遍历。总计列表,元组,字典,集合的联系与区别。

d={'001':'89','002':'94','003':'92','004':'87','005':'88','006':'86','007':'83'}
list=['01','02','03','04','05','06','07']
tuple=('4','2','6','7','3','7','9',)
s={'83874950'}
for i in d:
    print(i,d[i])
for i in list:
    print(i)
for i in tuple:
    print(i)
for i in s:
    print(i)

3、英文词频统计实例

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

 

news='''I'm tired of being what you want me to be
Feeling so faithless lost under the surface
Don't know what you're expecting of me
Put under the pressure of walking in your shoes
(Caught in the undertow just caught in the undertow)
Every step I take is another mistake to you
(Caught in the undertow just caught in the undertow)
I've become so numb I can't feel you there
I've become so tired so much more aware
I've becoming this all I want to do
Is be more like me and be less like you
Can't you see that you're smothering me
Holding too tightly afraid to lose control
Cause everything that you thought I would be
Has fallen apart right in front of you
(Caught in the undertow just caught in the undertow)
Every step that I take is another mistake to you
(Caught in the undertow just caught in the undertow)
And every second I waste is more than I can take
I've become so numb I can't feel you there
I've become so tired so much more aware
I've becoming this all I want to do
Is be more like me and be less like you
And I know
I may end up failing too
But I know
You were just like me with someone disappointed in you
I've become so numb I can't feel you there
I've become so tired so much more aware
I've becoming this all I want to do
Is be more like me and be less like you
I've become so numb I can't feel you there
Is everything what you want me to be
I've become so numb I can't feel you there
Is everything what you want me to be
'''
news=news.lower()
print(news)
for i in ',.':
    news=news.replace(i,'')
words=news.split(' ')
print(words)
dic={}
for i in words:
    dic[i]=news.count(i)
news=list(dic.items())
print('单词计数字典:',news,'/n')

转载于:https://www.cnblogs.com/kebibrany/p/7595946.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值