• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
J芷璇
博客园    首页    新随笔    联系   管理    订阅  订阅

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

1.实例: 下载一首英文的歌词或文章,将所有大写转换为小写,,将所有其他做分隔符(,.?!)替换为空格,并统计某些单词出现的次数。

 

s='''You were the shadow to my light
Did you feel us?
Another start
You fade away
Afraid our aim is out of sight
Wanna see us
Alive
Where are you now?
Where are you now?
Where are you now?
Was it all in my fantasy?
Where are you now?
Were you only imaginary?
Where are you now?
Atlantis
Under the sea
Under the sea
Where are you now?
Another dream
The monster's running wild inside of me
I'm faded
I'm faded
So lost, I'm faded
I'm faded
So lost, I'm faded
These shallow waters never met what I needed
I'm letting go a deeper dive
Eternal silence of the sea. I'm breathing alive
Where are you now?
Where are you now?
Under the bright but faded lights
You've set my heart on fire
Where are you now?
Where are you now?
Where are you now?
Atlantis
Under the sea
Under the sea
Where are you now?
Another dream
The monster's running wild inside of me
I'm faded
I'm faded
So lost, I'm faded
I'm faded
So lost, I'm faded'''
#大写转换为小写
s=s.lower()
print('大写变小写:',s)
#将(,?!)替换为空格
s=s.replace(',',' ')
print('分隔符转换为空格:',s)
s=s.replace('?',' ')
print('分隔符转换为空格:',s)
s=s.replace('!',' ')
print('分隔符转换为空格:',s)
#统计某个词的数量
s=s.count('now')
print('单词now出现的次数:',s)
#分隔出一个个单词
s=s.split(' ')
print('分隔结果:',s)

 

大写变小写: you were the shadow to my light
did you feel us?
another start
you fade away
afraid our aim is out of sight
wanna see us
alive
where are you now?
where are you now?
where are you now?
was it all in my fantasy?
where are you now?
were you only imaginary?
where are you now?
atlantis
under the sea
under the sea
where are you now?
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost, i'm faded
i'm faded
so lost, i'm faded
these shallow waters never met what i needed
i'm letting go a deeper dive
eternal silence of the sea. i'm breathing alive
where are you now?
where are you now?
under the bright but faded lights
you've set my heart on fire
where are you now?
where are you now?
where are you now?
atlantis
under the sea
under the sea
where are you now?
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost, i'm faded
i'm faded
so lost, i'm faded
分隔符转换为空格: you were the shadow to my light
did you feel us?
another start
you fade away
afraid our aim is out of sight
wanna see us
alive
where are you now?
where are you now?
where are you now?
was it all in my fantasy?
where are you now?
were you only imaginary?
where are you now?
atlantis
under the sea
under the sea
where are you now?
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
these shallow waters never met what i needed
i'm letting go a deeper dive
eternal silence of the sea. i'm breathing alive
where are you now?
where are you now?
under the bright but faded lights
you've set my heart on fire
where are you now?
where are you now?
where are you now?
atlantis
under the sea
under the sea
where are you now?
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
分隔符转换为空格: you were the shadow to my light
did you feel us
another start
you fade away
afraid our aim is out of sight
wanna see us
alive
where are you now
where are you now
where are you now
was it all in my fantasy
where are you now
were you only imaginary
where are you now
atlantis
under the sea
under the sea
where are you now
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
these shallow waters never met what i needed
i'm letting go a deeper dive
eternal silence of the sea. i'm breathing alive
where are you now
where are you now
under the bright but faded lights
you've set my heart on fire
where are you now
where are you now
where are you now
atlantis
under the sea
under the sea
where are you now
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
分隔符转换为空格: you were the shadow to my light
did you feel us
another start
you fade away
afraid our aim is out of sight
wanna see us
alive
where are you now
where are you now
where are you now
was it all in my fantasy
where are you now
were you only imaginary
where are you now
atlantis
under the sea
under the sea
where are you now
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
these shallow waters never met what i needed
i'm letting go a deeper dive
eternal silence of the sea. i'm breathing alive
where are you now
where are you now
under the bright but faded lights
you've set my heart on fire
where are you now
where are you now
where are you now
atlantis
under the sea
under the sea
where are you now
another dream
the monster's running wild inside of me
i'm faded
i'm faded
so lost i'm faded
i'm faded
so lost i'm faded
单词now出现的次数: 12

分隔结果: ['You', 'were', 'the', 'shadow', 'to', 'my', 'light\nDid', 'you', 'feel', 'us?\nAnother', 'start\nYou', 'fade', 'away\nAfraid', 'our', 'aim', 'is', 'out', 'of', 'sight\nWanna', 'see', 'us\nAlive\nWhere', 'are', 'you', 'now?\nWhere', 'are', 'you', 'now?\nWhere', 'are', 'you', 'now?\nWas', 'it', 'all', 'in', 'my', 'fantasy?\nWhere', 'are', 'you', 'now?\nWere', 'you', 'only', 'imaginary?\nWhere', 'are', 'you', 'now?\nAtlantis\nUnder', 'the', 'sea\nUnder', 'the', 'sea\nWhere', 'are', 'you', 'now?\nAnother', 'dream\nThe', "monster's", 'running', 'wild', 'inside', 'of', "me\nI'm", "faded\nI'm", 'faded\nSo', 'lost,', "I'm", "faded\nI'm", 'faded\nSo', 'lost,', "I'm", 'faded\nThese', 'shallow', 'waters', 'never', 'met', 'what', 'I', "needed\nI'm", 'letting', 'go', 'a', 'deeper', 'dive\nEternal', 'silence', 'of', 'the', 'sea.', "I'm", 'breathing', 'alive\nWhere', 'are', 'you', 'now?\nWhere', 'are', 'you', 'now?\nUnder', 'the', 'bright', 'but', 'faded', "lights\nYou've", 'set', 'my', 'heart', 'on', 'fire\nWhere', 'are', 'you', 'now?\nWhere', 'are', 'you', 'now?\nWhere', 'are', 'you', 'now?\nAtlantis\nUnder', 'the', 'sea\nUnder', 'the', 'sea\nWhere', 'are', 'you', 'now?\nAnother', 'dream\nThe', "monster's", 'running', 'wild', 'inside', 'of', "me\nI'm", "faded\nI'm", 'faded\nSo', 'lost,', "I'm", "faded\nI'm", 'faded\nSo', 'lost,', "I'm", 'faded']

 

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

zuoye=list('231231231213')
print('作业评分列表:',zuoye)
#查询第一个3分的下标
a=zuoye.index('3')
print(a)
#查询1分的有几个
b=zuoye.count('1')
print(b)
#查询3分的有几个
c=zuoye.count('3')
print(c)
#增加
d=zuoye.append('1')
print(zuoye)
#插入
e=zuoye.insert(3,'1')
print(zuoye)
#删除
f=zuoye.pop()
print(zuoye)

g=zuoye.pop(0)
print(zuoye)
#排序
h=zuoye.sort()
print('排序为:',zuoye)

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

    异:list是一种有序的序列,正向递增、反向递减序号;可以随时添加和删除其中的元素;没有长度限制、元素类型可以不同。

           tuple和list非常类似,但是tuple一旦初始化就不能修改。

    同:list和tuple是内置的有序集合,一个可变,一个不可变。

posted @ 2017-09-22 13:52  089江芷璇  阅读(311)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3