[Level 2]
Title: ocr
圖片下方提示
recognize the characters. maybe they are in the book,
but MAYBE they are in the page source.
在網(wǎng)頁源碼注釋中找到提示
find rare characters in the mess below:
再往下是一堆字符串解幼,根據(jù)提示是要找到字符串中出現(xiàn)次數(shù)較少的字符宁炫。嘗試找出其中的出現(xiàn)的字母:
target=''
for ch in s:#s是源碼中的字符串。
if ch.isalpha():
target+=ch
將數(shù)據(jù)放入文件中讀取或許會(huì)更好李皇。
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]))
利用字典計(jì)數(shù)空骚,取出所有出現(xiàn)次數(shù)小于平均值的字符,類似的方法(代碼未列出)是取出所有小于一定次數(shù)的字符驶忌。
1. [`collections.OrderedDict()`](https://docs.python.org/3/library/collections.html?highlight=collections.ordereddict#collections.OrderedDict)返回字典的子類實(shí)例矛辕,該子類記住了Key插入的順序。
2. [`dict.get(key[, default])`](https://docs.python.org/3/library/stdtypes.html?highlight=dict.get#dict.get)返回字典中鍵對(duì)應(yīng)的值,不存在且沒有指定默認(rèn)值如筛,返回None堡牡,否則返回指定值。
####2.
> ```python
mess = open("data.txt").read()
print(mess.translate(str.maketrans('','','%$()[]{}+_@^!*&#\n')))
或者使用集合杨刨,利用集合的特性把數(shù)據(jù)添加到集合中去重晤柄。