跳到正文
伩仁的博客
返回

自己需要记住和时常复习的python要点和小技巧

不定时更新中, 20141007最新更新

from collections import Counter


def checkio(text):
    count = Counter([x for x in text.lower() if x.isalpha()])
    m = max(count.values())
    return sorted([x for (x, y) in count.items() if y == m])[0]

if __name__ == "__main__":
    print(checkio("Hello World!"))
    print(checkio("How do you do?"))
    print(checkio(u"One"))
    print(checkio(u"Oops!"))

counter的方法有增加(updata)和减少(subtract)两种: 比如:

count = Counter('which')
count['h']
 #结果显示:2
count.update('witch')
count.['h'] 
 #结果显示:3

B(括号)

在正则表达式中有 3 种类型的括号 方括号”[“内是需要匹配的字符,花括号”{“内是指定匹配字符的数量。 圆括号“(“ 则是用来分组的。

C(插入符号)

插入符号 “^” 表示正则式的开始。

D(美元符号)

美元符号“$” 表示正则式的结束。

注意:类似(com|net|org)的多选一,用圆括号.

年月日的匹配: yy-mm-dd

'^(1[9][0-9][0-9]|2[0][0-1][0-9]|[0-9][0-9])[\.-]?([1-9]|0[1-9]|1[0-2])[\.-]?([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])$'

时间的匹配: hh:mm:ss

'^([0-9]|0[0-9]|1[0-9]|2[0-3])[:]([0-9]|0[0-9]|[0-6][0-9])[:]([0-9]|0[0-9]|[0-5][0-9])$'

ip地址的匹配:

 '^(([01]?[0-9][0-9]?|2[0-4][0-9]|2[5][0-5])\.){3}([01]?[0-9][0-9]?|2[0-4][0-9]|2[5][0-5])$'


上一篇
sublime text3 安装和设置
下一篇
win10下面python的codecs模块写入txt文件,用记事本打开显示不换行bug