这个是 Python Challenge 的 Level 2。
recognize the characters. maybe they are in the book, but MAYBE they are in the page source.
网页上也明确的给出了General tips:
很明显,需要去查阅网页源代码。直接查看源代码,发现还真有:
...<!-- find rare characters in the mess below: --><!-- %%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{* @##&{#&{&)*%(]{{([*}@[@&]+!!*{)!}{%+{))])[!^})+)$]#{*+^((@^@}$[**$&^{$!@#$%)!@(& +^!{%_$&@^!}$_${)$_#)!({@!)(^}!*^&!$%_&&}&_#&@{)]{+)%*{&*%*&@%$+]!*__(#!*){%&@++ !_)^$&&%#+)}!@!)&^... -->
很明显,是要找出下面一大堆mess里面的字母们。
刚开始没关注到上面的注释,以为是做字频统计,代码如下:
# coding=utf-8 # ocr string = "%%$@_$^__#)^)&!_+]!*@&..." # 太长了,文章贴不下,略去 string_dict = {} for x in string: if x not in string_dict: string_dict[x] = 0 while string: x = string[0] origin_len = len(string) string = string.replace(x, '') string_dict[x] = origin_len - len(string) print(string_dict)
运行结果:
{'!': 6079, '#': 6115, '%': 6104, '$': 6046, '&': 6043, ')': 6186, '(': 6154, '+': 6066, '*': 6034, '@': 6157, '[': 6108, ']': 6152, '_': 6112, '^': 6030, 'a': 1, 'e': 1, 'i': 1, 'l': 1, 'q': 1, 'u': 1, 't': 1, 'y': 1, '{': 6046, '}': 6105}
看到这个结果才意识到是找单词:
# string 太长了,略去 for x in string: if not x.isalpha(): string = string.replace(x, '') print(string)
运行结果:
equality
最佳答案1:
print "".join([char for char in text if char.isalpha()])
最佳答案2:
import string filter(lambda x: x in string.letters, text)
点评:
本文分享自微信公众号 - 趣Python(yang-an-china),作者:杨安
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2018-02-11
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句