Skip to content

Instantly share code, notes, and snippets.

@shiumachi
Created January 3, 2014 06:56
Show Gist options
  • Select an option

  • Save shiumachi/8233944 to your computer and use it in GitHub Desktop.

Select an option

Save shiumachi/8233944 to your computer and use it in GitHub Desktop.
任意の文字列 line に word が含まれているかどうかを判定し、集計する。複数の word があっても効率よく集計できる
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