Python Challenge(2)

続いて第2問
ソースに「find rare characters in the mess below」とあるので文字毎の出現回数を数えてみる。

def count(str):
    counter = {}
    for s in str:
        counter[s] = counter.get(s, 0) + 1
    print(counter)

if __name__ == '__main__':
    count(open('challenge2.txt').read())
$ python challenge002.py
{'\n': 1220, '!': 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}

1回しか出てこない文字を抽出すりゃええのねってことで、

def only_once(str):
    counter = {}
    for s in str:
        counter[s] = counter.get(s, 0) + 1
    print(''.join([s for s in str if counter[s] == 1]))

if __name__ == '__main__':
    only_once(open('challenge2.txt').read())

抽出するのはアルファベットなので

def only_once2(str):
    from string import ascii_lowercase
    print(''.join([s for s in str if s in ascii_lowercase]))

でもいいか。
答えをURLのファイル名部分に当てはめて〆