在GitHub上发现一些很有意思的项目,由于本人作为Python的初学者,编程代码能力相对薄弱,为了加强Python的学习,特此利用前辈们的学习知识成果,自己去亲自实现。

一周没有更新了,主要还是自己太懒,三分热度,我想如果我继续这样下去,做什么事都做不好,以此为戒,坚持一个月内练习完这些知识点!

来源:GitHub
Python练手小程序项目地址:https://github.com/Show-Me-the-Code/python
写作日期:2019.12.08

今天练习第0004题,题目如下:

这道题主要考察细节问题,比如:It’s/wasn’t/don’t……这类缩写的词怎么检测到,以及还原。

英文文件subtitle.txt,内容如下:

Make sure you know what
you're supposed to be doing. Okay, that sounds obvious and easy, right? This is not always true. If you are new to your job, you might not
have a feel for your responsibilities. If you have not done a certain
type of work before, you might not know how long
something is going to take. That's completely normal. So how do you know? Ask. Review your job responsibilities,
ask someone who is in the same roll or who has previously done this job, keep good
notes, keep track of how long something takes you so that you have a solid
estimate and you can use that next time. Remember, it also helps to take big
tasks and break them into smaller steps. Remember that research Sam was
starting for the sales reports? He might take that research and divide it
into steps, that way he can keep track of each step and he'll know what is
involved in completing that research. How do you know what your priorities are? Depending on the type of work you do,
your priorities come to you from your leadership or perhaps from a customer
who is, you're supporting. It's important to really understand
the priorities you're expected to support. So in your sample work plan, you're going
to have something, perhaps the priority, the description of what you're doing,
the due date, who you're doing it for, the steps, an estimate of how long
it's going to take, and a start date. And those are some good fields for
you to start out with. And you can create a table,
a spread sheet, and begin to work with this and see if this
helps you in completing your work. You might not really know how
long something is going to take. That's okay. So how do you know? Ask. Review your job responsibilities
with your supervisor, ask someone who's in the same role,
or who has previously done the job. Keep good notes when you do something,
keep notes, keep track of how long it takes you, and then you have something to refer back
to for a good estimate next time. Remember, it also helps to take big
tasks and break them into smaller steps. Remember when Sam was
working on that research, he started that research
with those sales reports? He might take that research and
divide it into steps, and that way he can keep track of
how long each step will take, and he's gonna know what is involved
in completing that research. How do you know what your priorities are? Well, depending on the type of work you
do, sometimes your priorities are gonna come to you from your leadership or
from a customer you support. It's important to really understand the
priorities you are expected to support. In our example with Sam,
it became more important for him to complete his status report
first and then his other report later. What if your boss can not or
will not tell you? You know sometimes the person you work for
wants to see you figure it out for yourself. Sometimes the person you work for doesn't
really know what the true priorities are. And if this is true, you're gonna
have to figure it out for yourself. Pay attention to what is discussed the
most in meetings and in announcements and in other communications. Pay attention to where your successful
colleagues are spending their time. There are clues around you waiting for
you to uncover them. Before you go,
I would like to share with you a story. I once had a co-worker who had
an important meeting after lunch and the meeting was with an executive. She was gonna facilitate the meeting and
give a presentation. During lunch, she remembered that she forgotten
to pick something up at the store. The item she forgot was something
her family needed that evening. Immediately, she ran out of the office to
the store and purchased the missing item. In doing so,
she was late to her own meeting and to make matters worse,
she explained why she was late. Her manager looked at her and said, if I had known you were gonna go do
that, I would've gone to the store for you so you could have been here
on time and been prepared. That's what we call
a career limiting moment. It was all because she forgot her plan,
she forgot her priorities, and she forgot the difference between something
that was urgent versus important. It may have been important that she bring
home that item that evening to her family, but it wasn't urgent. She could've picked it up
on the way home from work. What was both urgent and important
was being prepared for that meeting. Now, before we move to the next module,
why don't you consider taking a shot at creating your own plan that covers
at least the next five business days.

想要统计英文文件单词出现的个数:

  1. 首先检测缩写单词,我们使用正则表达,用'/''遍历每一个单词元素去检测。
  2. 缩写单词还原,根据规则使用穷举法即可。
  3. 统计单词个数,使用字典。
  4. 根据个数大小,对字典进行排序。

Python处理代码如下:

import stringdef extend_word(text):"""文件内容缩写检测和缩写还原:param text::return:"""# 判断文件内容text中是否存在缩写if text.find('\'') > 0:old2new = dict()words = text.split()for i, word in enumerate(words):# 判断每个单词元素是否存在缩写if word.find('\'') > 0:# 对缩写单词元素进行分隔成两部分parts = word.split('\'')# 根据规则穷举所有可能的缩写单词元素,并还原if parts[1] == 'm':parts[1] = 'am'elif parts[1] == 's':parts[1] = 'is'elif parts[1] == 're':parts[1] = 'are'elif parts[1] == 't':parts[1] = 'not'elif parts[1] == 've':parts[1] = 'have'elif parts[1] == 'll':parts[1] = 'will'# 如果英文缩写是'd',那么需要考虑后面的单词,若是'better',那么是'had',否则是'would'elif parts[1] == 'd':if words[i + 1] == 'better':parts[1] = 'had'else:parts[1] = 'would'# 针对don't/doesn't等这些单词元素的还原if parts[0].endswith('n'):parts[0] = parts[0][:-1]# 每个缩写的单词元素对应的还原后的单词组# 备注一下,比如:I'd better/ I'd like ,这两个同时存在的话,键对应的值就不是唯一的,后者出现的值会覆盖前者,这里我没做考虑old2new[word] = ' '.join(parts)_text = text# print(old2new)# 对英文文件中的缩写单词替换成还原后的单词组for old_word in old2new.keys():_text = _text.replace(old_word, old2new[old_word])return _textreturn textdef count_num():"""去除特殊字符,并统计英文单词个数:return:"""with open('subtitle2.txt', 'r') as file:article = file.read()no_pun_text = article# string.punctuation 返回 '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'# 去掉'''_punctuation = string.punctuation.replace('\'', '')# 去掉英文文件内容中的特殊字符for pun in _punctuation:no_pun_text = no_pun_text.replace(pun, '')complete_text = extend_word(no_pun_text)records = dict()for word in complete_text.lower().split():records[word] = records.get(word, 0) + 1return recordsdef show_in_order(records):"""对字典根据value的取值大小排序:param records::return:"""items = sorted(records.items(), key=lambda x: x[1], reverse=True)for item in items:print(item[0], item[1])if __name__ == '__main__':records = count_num()show_in_order(records)

Python练手小程序—统计英文文件中单词出现的的个数相关推荐

  1. python小程序源代码-整理了适合新手的20个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. 本文附带基础视频教程:私信回复[基础]就可以获取的 [程序1] ...

  2. python小程序-整理了适合新手的20个Python练手小程序

    即刻关注公众号,发现世界的美好 100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3 ...

  3. python编程100个小程序-【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

  4. python编程100个小程序-整理了适合新手的20个Python练手小程序

    即刻关注公众号,发现世界的美好 100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3 ...

  5. python练手经典100例-【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

  6. python小程序-【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

  7. python练手小程序—调整图片分辨率(大小)

    在GitHub上发现一些很有意思的项目,由于本人作为Python的初学者,编程代码能力相对薄弱,为了加强Python的学习,特此利用前辈们的学习知识成果,自己去亲自实现. 一周没有更新了,主要还是自己 ...

  8. 【Python精华】100个Python练手小程序(Python3 已亲测)

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1]  题目:有1.2.3.4个数字,能组成多少个互不相 ...

  9. 统计英文文件中单词出现频率

    统计一个文件里每个单词出现的次数 #1.打开指定文件 #2.删除除了单词之外所有标点符号,并将每个单词分割开 #3.统计每个单词出现次数

最新文章

  1. 算法-----数组------ 数组中的第K个最大元素
  2. 将win7电脑变身WiFi热点,让手机、笔记本共享上网
  3. 华师大计算机入门模拟卷,计算机入门模拟卷A-华东师范大学.docx
  4. python之控制流习题+代码
  5. ArcGIS JS先添加动态图层,再添加切片图层后不显示
  6. 利用正则表达式实现python强口令检测
  7. pytorch无法将模型加载到gpu上
  8. spring消息队列_AmazonSQS和Spring用于消息传递队列
  9. 618期间, “直播带货”翻车负面信息暴增
  10. 石家庄医学高等专科学校计算机二级,石家庄人民医学高等专科学校2021年排名...
  11. linux内核程序运行在哪里,linux内核 – 设备驱动程序代码在哪里执行?内核空间还是用户空间?...
  12. 分类战车SVM全系列
  13. TCN机器之心的转载,后面需要实现
  14. android下拉水波纹,android自定义WaveView水波纹控件
  15. C语言tolower和toupper的用法
  16. VScode运行时提示找不到应用程序
  17. 学渣的刷题之旅 leetcode刷题 100.相同的树
  18. 从面试官的角度聊聊培训班对程序员的帮助,同时给培训班出身的程序员一些建议...
  19. python3下载-QPython3下载
  20. 动态获取Bing每日壁纸

热门文章

  1. 小牛电动车09Z和10Z的区别
  2. 查看linux centos服务器配置-常用命令
  3. 微信小程序+vue+taro:搜索框
  4. ps常用的扣图工具有哪些,都有哪些方法
  5. 毕业论文系统的设计类图
  6. icloud android同步到iphone6s,安卓手机竟能同步苹果iCloud:啥黑科技?
  7. linux luks分区加密,Linux下分区加密LUKS
  8. Andorid 自定义标题栏
  9. 怎么PDF转Excel?推荐这几款软件给你
  10. 【word】正文双栏,尾注只占页面半边