pythonchallenge第二关

http://www.pythonchallenge.com/pc/def/ocr.html

用python抓取网页内容,用正则表达式匹配出相应的内容,让后找出出现最少的字符,并按出现的先后顺序排序即可


import sys

import urllib
import re

str = urllib.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html").read()
match = re.findall(r"", str, re.S)
time = {}
i = 1;
for each in match[1]:
    if each in time:
        time[each][0] += 1
    else:
        temp = []
        temp.append(1)
        temp.append(i)
        time[each] = temp
        i += 1
t1 = sorted(time.iteritems(), key=lambda x:x[0])
t2 = sorted(t1, cmp=lambda x, y:cmp(x[1][0], y[1][0]))
t3 = []
num = t2[0][1][0]
for each in t2:
    if each[1][0]==num:
        t3.append(each)
    else:
        break
t4 = sorted(t3, cmp=lambda x, y:cmp(x[1][1], y[1][1]))
print(t4)


你可能感兴趣的:(pythonchallenge)