美文网首页
Python Challenge[2]

Python Challenge[2]

作者: Recgat | 来源:发表于2017-02-06 17:33 被阅读0次

[Level 2]


Title: ocr

图片下方提示

recognize the characters. maybe they are in the book,
but MAYBE they are in the page source.

在网页源码注释中找到提示

find rare characters in the mess below:

再往下是一堆字符串,根据提示是要找到字符串中出现次数较少的字符。尝试找出其中的出现的字母:

target=''
for ch in s:#s是源码中的字符串。
  if ch.isalpha():
    target+=ch

将数据放入文件中读取或许会更好。

with open('ocr.txt', encoding='utf-8') as f:
    s = f.read()

得到equality,试了下正是我们需要的,[Level 3]

Python Challenge Wiki

1.

s = ''.join([line.rstrip() for line in open('ocr.txt')])
OCCURRENCES = {}#OCCURRENCES = collections.OrderedDict()
for c in s: OCCURRENCES[c] = OCCURRENCES.get(c, 0) + 1
avgOC = len(s) // len(OCCURRENCES)
print(''.join([c for c in s if OCCURRENCES[c] < avgOC]))


利用字典计数,取出所有出现次数小于平均值的字符,类似的方法(代码未列出)是取出所有小于一定次数的字符。
1. [`collections.OrderedDict()`](https://docs.python.org/3/library/collections.html?highlight=collections.ordereddict#collections.OrderedDict)返回字典的子类实例,该子类记住了Key插入的顺序。
2. [`dict.get(key[, default])`](https://docs.python.org/3/library/stdtypes.html?highlight=dict.get#dict.get)返回字典中键对应的值,不存在且没有指定默认值,返回None,否则返回指定值。

####2. 
> ```python
mess = open("data.txt").read()
print(mess.translate(str.maketrans('','','%$()[]{}+_@^!*&#\n')))

或者使用集合,利用集合的特性把数据添加到集合中去重。

More

相关文章

  • Python挑战:00~03关

    Python Challenge Python Challenge 00 网址: http://www.pytho...

  • The Python Challenge(2)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 将页面给定的字符串根据给定规则进行替换即可,规则如下...

  • Python Challenge[2]

    [Level 2] Title: ocr 图片下方提示 recognize the characters. may...

  • Python挑战:04-05关

    Python Challenge Python Challenge 04 现在,我们来挑战第四关,从第三关的结果,...

  • Python Challenge 第2关

    第2关 提示信息,在书或页面中找出字符。答案应该藏在页面源码中。 如下图所示,下载内容中提示在下方内容中找到稀有字...

  • The Python Challenge(5)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 根据页面源码提示: 再点击页面图片显示: 可知是需要...

  • The Python Challenge(8)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 页面和源码中无任何提示,但图片中有一条很明显的灰度线...

  • The Python Challenge(9)

    问题链接 问题链接如下: 答案链接 答案链接如下: 登陆用户名密码为huge和file。 解题思路 阅读源码有如下...

  • The Python Challenge(3)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 根据页面提示: 阅读源码,有如下内容: 编写代码从中...

  • The Python Challenge(4)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 根据页面提示: 并结合页面源码中的内容,有如下代码:...

网友评论

      本文标题:Python Challenge[2]

      本文链接:https://www.haomeiwen.com/subject/dlsuittx.html