life timeã¨ããã®ã¯çåæéã¨è¨³ã§ãããã¤ã¾ãããä¸å®ã®æ°å¤åã«ãã©ããããã®é·ããå± ããã¨ãããã¨ã測ãã¾ãããã®è§£æçµæã¯ãã©ã®æ°å¤åããã¨ããå®ç¾©ã«ä¾åãã¾ãã
# time value 0 3 1 2 2 3 3 1 4 0 5 1 6 4
ãã¨ãã°ãããããã¼ã¿ããã£ãã¨ãã¦ãvalueã1以ä¸ã®æã®life timeãç¥ããããªãæå»3~5ã§1,0,1ã¨å¤ãã£ã¦ããã®ã§life timeã¯"3"ã¨ãããã¾ãã2以ä¸ã¨ãããªããæå»1ã®æã«2ãããã®ã§ãlife timeã¯[1, 3]ã¨ããé åã«ãªãã¾ãã
ä»åã¯ãããã±ã±ã£ã¨è§£æãã¦ãããpythonã¹ã¯ãªãããæ¸ãã¾ãããã»ã¨ãã©åã®ã¤ãã®ä½¿ãåãã ãã
使ãæ¹ã¯ã
python lifetime.py <input> <ncolumn> <threshold>
å ¥åãã¼ã¿ãã¡ã¤ã«åãåçªå·ãå®ç¾©åãæå®ãã¾ããä»ã®ã¨ãããå®ç¾©åã¯ãããå®æ°å¤ä»¥ä¸ãã¨ããå½¢ã«ãªã£ã¦ããã®ã§ç¯å²æå®ã§ã¯ãªã解æå¤ã®æ大å¤ãæå®ããå½¢ã§ãã
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import os if __name__ == '__main__': argvs = sys.argv argvc = len(argvs) if argvc != 4: print "python lifetime.py <input> <ncolumn> <threshold>" exit() else: infile = argvs[1] ncol = int(argvs[2]) th = float(argvs[3]) if not(os.path.exists(infile)): print "%s is NOT found !!" % infile exit() else: cnt = 0 nl = 0 f = open(infile, 'r') for line in f: nl += 1 s = line[:-1].strip() if s == "" or s[0] == "@" or s[0] == "#": pass else: col = line[:-1].split() if len(col) < ncol: print "%s\n" % line print "line[%d] is NOT enought column number" % nl exit() else: if float(col[ncol-1]) < th: if cnt == 0: print "%s " % line[:-1], cnt += 1 else: if cnt > 0: print cnt cnt = 0 f.close()