Created
January 3, 2014 06:56
-
-
Save shiumachi/8233944 to your computer and use it in GitHub Desktop.
任意の文字列 line に word が含まれているかどうかを判定し、集計する。複数の word があっても効率よく集計できる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import sys | |
| import logging | |
| logging.getLogger().setLevel(logging.DEBUG) | |
| d = {} | |
| def count(line, word): | |
| #logging.debug("line: {0}".format(line)) | |
| if word not in d: | |
| restr = ".*{0}.*".format(word) | |
| logging.debug(restr) | |
| d[word] = re.compile(restr) | |
| #logging.debug("d['{1}'] = {0}".format(d[word], word)) | |
| if re.match(d[word],line): | |
| #logging.debug("d[word].match(line) = {0}".format(d[word].match(line))) | |
| #logging.debug("{0} match".format(word)) | |
| return 1 | |
| return 0 | |
| if __name__=='__main__': | |
| c = 0 | |
| for line in sys.stdin: | |
| c += count(line, ':56') | |
| print("SPLIT: {0}".format(c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment