博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
英文词频统计预备,组合数据类型练习
阅读量:4634 次
发布时间:2019-06-09

本文共 4402 字,大约阅读时间需要 14 分钟。

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

s='''Twinkle, twinkle, little star,How I wonder what you are.Up above the world so high,Like a diamond in the sky.Twinkle, twinkle, little star,How I wonder what you are!When the blazing sun is gone,When he nothing shines upon,Then you show your little light,Twinkle, twinkle, all the night.Twinkle, twinkle, little star,How I wonder what you are!Then the traveler in the darkThanks you for your tiny spark;He could not see which way to go,If you did not twinkle so.Twinkle, twinkle, little star,How I wonder what you are!Twinkle Twinkle Little Star'''s=s.lower()print('大写转换为小写:'+s)for i in ',.!':    s=s.replace(i,' ')print('所有标点符号替换为空格:'+s)print('twinkle出现次数:',s.count("twinkle"))print('歌词出现的单词:',s.split(' '),'\n')
s='''Twinkle, twinkle, little star,How I wonder what you are.Up above the world so high,Like a diamond in the sky.Twinkle, twinkle, little star,How I wonder what you are!When the blazing sun is gone,When he nothing shines upon,Then you show your little light,Twinkle, twinkle, all the night.Twinkle, twinkle, little star,How I wonder what you are!Then the traveler in the darkThanks you for your tiny spark;He could not see which way to go,If you did not twinkle so.Twinkle, twinkle, little star,How I wonder what you are!Twinkle Twinkle Little Star'''s=s.lower()print('大写转换为小写:'+s)for i in ',.!':    s=s.replace(i,' ')print('所有标点符号替换为空格:'+s)print('twinkle出现次数:',s.count("twinkle"))print('分隔单词:',s.split(' '),'\n')
大写转换为小写:twinkle, twinkle, little star,how i wonder what you are.up above the world so high,like a diamond in the sky.twinkle, twinkle, little star,how i wonder what you are!when the blazing sun is gone,when he nothing shines upon,then you show your little light,twinkle, twinkle, all the night.twinkle, twinkle, little star,how i wonder what you are!then the traveler in the darkthanks you for your tiny spark;he could not see which way to go,if you did not twinkle so.twinkle, twinkle, little star,how i wonder what you are!twinkle twinkle little star所有标点符号替换为空格:twinkle  twinkle  little star how i wonder what you are up above the world so high like a diamond in the sky twinkle  twinkle  little star how i wonder what you are when the blazing sun is gone when he nothing shines upon then you show your little light twinkle  twinkle  all the night twinkle  twinkle  little star how i wonder what you are then the traveler in the darkthanks you for your tiny spark;he could not see which way to go if you did not twinkle so twinkle  twinkle  little star how i wonder what you are twinkle twinkle little startwinkle出现次数: 13分隔单词: ['twinkle', '', 'twinkle', '', 'little', 'star', '\n\nhow', 'i', 'wonder', 'what', 'you', 'are', '\n\nup', 'above', 'the', 'world', 'so', 'high', '\n\nlike', 'a', 'diamond', 'in', 'the', 'sky', '\n\ntwinkle', '', 'twinkle', '', 'little', 'star', '\n\nhow', 'i', 'wonder', 'what', 'you', 'are', '\n\nwhen', 'the', 'blazing', 'sun', 'is', 'gone', '\n\nwhen', 'he', 'nothing', 'shines', 'upon', '\n\nthen', 'you', 'show', 'your', 'little', 'light', '\n\ntwinkle', '', 'twinkle', '', 'all', 'the', 'night', '\n\ntwinkle', '', 'twinkle', '', 'little', 'star', '\n\nhow', 'i', 'wonder', 'what', 'you', 'are', '\n\nthen', 'the', 'traveler', 'in', 'the', 'dark\n\nthanks', 'you', 'for', 'your', 'tiny', 'spark;\n\nhe', 'could', 'not', 'see', 'which', 'way', 'to', 'go', '\n\nif', 'you', 'did', 'not', 'twinkle', 'so', '\n\ntwinkle', '', 'twinkle', '', 'little', 'star', '\n\nhow', 'i', 'wonder', 'what', 'you', 'are', '\n\ntwinkle', 'twinkle', 'little', 'star']

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

fenshu=list("12312122312")print('分数:',fenshu)print('列表长度:',len(fenshu))print('列表最大值:',max(fenshu))print('列表最小值:',min(fenshu))fenshu.append('2')fenshu.insert(1,'3')fenshu.pop(2)print('输出增插删后列表:',fenshu)fenshu=[int(x) for x in fenshu]print('更改为数值型:',fenshu)print('第一个3分的下标:',fenshu.index(3))print('1分的同学人数:',fenshu.count(1))print('3分的同学人数:',fenshu.count(3))
分数: ['1', '2', '3', '1', '2', '1', '2', '2', '3', '1', '2']列表长度: 11列表最大值: 3列表最小值: 1输出增插删后列表: ['1', '3', '3', '1', '2', '1', '2', '2', '3', '1', '2', '2']更改为数值型: [1, 3, 3, 1, 2, 1, 2, 2, 3, 1, 2, 2]第一个3分的下标: 11分的同学人数: 43分的同学人数: 3

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

异:列表(list)函数新增的元素,会改变列表本身;没有长度限制、元素类型可以不同;可以随时进行增删改操作。

       元组(tuple)一旦初始化,不能进行增删改和排序的操作。

同:都是有序的序列。

转载于:https://www.cnblogs.com/00js/p/7576519.html

你可能感兴趣的文章
浅谈WPF的VisualBrush
查看>>
CSS------当内容超出div宽度后自动换行和限制文字不超出div宽度和高度
查看>>
经常用得到的安卓数据库基类
查看>>
简单入门dos程序
查看>>
vue element 关闭当前tab 跳转到上一路由
查看>>
4、面向对象
查看>>
[NOI2005]聪聪与可可(期望dp)
查看>>
POJ 3723
查看>>
Maven的安装
查看>>
angular初步认识一
查看>>
springmvc3.2+spring+hibernate4全注解方式整合(一)
查看>>
Elgg网站迁移指南
查看>>
素数筛法优化
查看>>
installshield 注册dll
查看>>
Sublime Text 3 及Package Control 安装(附上一个3103可用的Key)
查看>>
LTE QCI分类 QoS
查看>>
Get MAC address using POSIX APIs
查看>>
bzoj2120
查看>>
基于uFUN开发板的心率计(一)DMA方式获取传感器数据
查看>>
【dp】船
查看>>