Python基础:项目实战(一)计算文章难度

钢铁意志会赋予勇者


声明

仅作为个人学习使用,仅供参考


Question

一篇英文文章中有很多单词,初始难度分为 0 ,现给你一个生词表

对照这个生词表,每当出现一次生词就将难度分 +5,最终的难度分就代表这篇文章的难度分数

article = '''This is a photograph of our village.
Our village is in a valley.
It is between two hills.
The village is on a river.
Here is another photograph of the village.
My wife and I are walking along the banks of the river.
We are on the left.
There is a boy in the water.
He is swimming across the river.
Here is another photograph.
This is the school building.
It is beside a park.
The park is on the right.
Some children are coming out of the building.
Some of them are going into the park.
'''

new_words = [
  'photograph',
  'village',
  'valley',
  'between',
  'hills',
  'another',
  'prep',
  'wife',
  'along',
  'banks',
  'water',
  'swimming',
  'building',
  'park',
  'into'
]

问题分解

  1. 统计文章中的单词个数
  2. 对照生词表,统计文章中的生词的个数
  3. 根据生词个数计算难度分
  4. 优化代码  =>  函数封装 def

统计文章中的单词个数

观察生词表中的单词首字母都是小写 而文章中的就不一样了

因此需要文本的处理

article = 文本在此处省略,自行查阅上文

ams = {}
article_list= article.lower().replace('.','').split()

for i in article_list:
    if i in ams:
        ams[i] += 1
    else:
        ams[i] = 1
print(ams)


输出:
{'this': 2, 'is': 11, 'a': 5, 'photograph': 3, 'of': 5, 'our': 2, 'village': 4, 'in': 2, 'valley': 1, 'it': 2, 'between': 1, 'two': 1, 'hills': 1, 'the': 12, 'on': 3, 'river': 3, 'here': 2, 'another': 2, 'my': 1, 'wife': 1, 'and': 1, 'i': 1, 'are': 4, 'walking': 1, 'along': 1, 'banks': 1, 'we': 1, 'left': 1, 'there': 1, 'boy': 1, 'water': 1, 'he': 1, 'swimming': 1, 'across': 1, 'school': 1, 'building': 2, 'beside': 1, 'park': 3, 'right': 1, 'some': 2, 'children': 1, 'coming': 1, 'out': 1, 'them': 1, 'going': 1, 'into': 1}


article_list= article.lower().replace('.','').split()

这行代码执行的顺序是从左往右的,这点是需要注意的

ams[i] += 1
ams[i] += 1不需要将i录入字典,Python会直接将i导入字典 并且它的值从0开始计算

对照生词表,统计文章中的生词的个数

目前,我们已经得到了统计文章单词个数的字典 即 ams

sum = 0
for i in new_words:#文章中不一定绝对会出现所有生词,因此要判断
    if i in ams:
        sum += ams[i]
print(sum*5)

输出:
115 即难度分数

遍历生词表 在字典中对应键名 相应的值 连加 即可

函数封装 def

用自定义函数来表现,可以参考下文代码

Code

def get_word_count(article): #获得ams值
    ams = {}
    article_list= article.lower().replace('.','').split()

    for i in article_list:
        if i in ams:
           ams[i] += 1
        else:
            ams[i] = 1
    return  ams



def get_difficulty(ams,new_words):
    sum = 0
    for i in new_words:
        if i in ams:
            sum += ams[i]
    return sum*5

#get_word_count(article) 即为ams

print(get_difficulty(get_word_count(article),new_words))

get_word_count(artle) 返回的是 ams 值

在调用第二个自定义函数的第一个参数就是 ams , 这样就好理解了。

最后得到  答案 难度分为  115 


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值