英文词频统计预备,组合数据类型练习

本文通过实例展示了如何使用Python进行数据处理任务,包括文本清洗、单词计数及列表的操作。同时对比了列表与元组的特点和使用场景。

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

1.实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。

ordinary='''This is just an ordinary day,
Wipe the insecurities away. 
I can see that the darkness will erode, 
Looking out the corner of my eye,.
I can see that the sunshine will explode,
Far across the desert in the sky. 
Beautiful girl won't you be my inspiration, 
Beautiful girl don't you throw your love around.
What in the world, what in the world could ever come between us,
beautiful girl, beautiful girl. 
I'll never let you down, won't let you down, 
This is the beginning of your day. 
Life is more intricate than it seems, 
Always be yourself along the way. 
Living through the spirit of your dreams.'''
for i in ",.?!":
    ordinary=ordinary.replace(i,' ')
ordinary=ordinary.replace('\n',' ')
words=ordinary day.split(' ')
print(words)
print("ordinary出现的次数:"+str(words.count("ordinary")))
print("girl出现的次数:"+str(words.count("girl")))

 

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

 

score=list('33321131232321232')
print(score)
for i in range(len(score)):
    score[i-1]=int(score[i-1])
for i in range(len(score)):
    if(score[i-1]==3):
        print("第一个3分的下标为:"+str(i-1))
        break
print("得2分的同学有"+str(score.count(2))+"")
 
print("删除列表下标为4的值")
score.pop(4)
print(score)
 
print("追加一个值“6”")
score.append(6)
print(score)
 
print("在列表下标第1位插入一个8")
score.insert(1,8)
print(score)

 

3.简要描述列表与元组的异同。

列表是Python的一种内置数据类型,list是一种有序的集合,可以随时添加和删除其中的元素。元组也是一种有序列表,和list非常类似,不同点是tuple一旦定义了就不可修改,在一定意义上这也提高了代码的安全性,查询方法和list一样,使用的时候能用tuple的就用tuple。列表和元组非常类似,有时候他们都干一样的事情。他们最大的区别是元组一旦被赋值,值不可以被改变,一旦改变就会出错;但是列表可以任意的更改。其次的区别是他们用不同的符号表示,复制的时候,列表用方括号[],而元组用小括号()。

 

转载于:https://www.cnblogs.com/xy223/p/7576849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值