Python Challenge[2]

[Level 2]

Python Challenge[2]_第1张图片

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 Challenge[2])