---
title: "python challenge 2"
author: "he_lux"
date: "2009-12-28T17:50:22Z"
tags: []
categories: ["编程语言"]
slug: "python-challenge-2"
draft: false
lastmod: "2009-12-28T17:55:37Z"
id: 558481
url: "https://hhlux.iteye.com/blog/558481"
---

根据网页上的提示，打开网页源代码，有一句：find rare characters in the mess below:
意思是找出出现次数非常少的字母。于是统计这段mess中的每个字符出现的次数。

其实像%$@_这些都可以不用统计了，应该提示的是找出characters。

查出aeilquty这几个字母出现的次数是1,组合出来的单词有equality，这个词就是答案了。

```python

import re

if __name__ == '__main__':
    
    # put the mess from the page source into 2.txt
    f = open('2.txt', 'r')
    text = f.read()
    
    #solution 1 Start
    dic = {}
    
    for x in text:
        if x in dic:
            dic[x] += 1
        else:
            dic[x] = 1
                
    print('solution 1 :')
    for x in dic:
        print '%s, %d' % (x, dic[x])    
    #solution 1 End
        
    #solution 2 Start
    dict = {}
    
    for x in text:
        dict[x] = dict.get(x, 0) + 1;
            
    print('solution 2:')
    print(''.join([x for x in dict if dict[x] < 10]))
    #solution 2 End
    
    #solution 3 Start
    print('solution 3:')
    print ''.join([x for x in text if x.isalpha()])
    #solution 3 End
    
    #solution 4 Start
    print('solution 4:')
    print(re.findall('[a-z]', text))
    #solution 4 End
    
    f.close();

```

学习了re模块的使用。