關於python中的數學函數的體會:python
其實就是一個很小很小的學習心得。由於自己沒有很是好好地學習其餘編程語言,也沒經驗,就把本身學習的東西隨時記錄下來。編程
當我用在循環中用min取最小值時,比用條件句去比較,效率更高。編程語言
附上很簡短的代碼:函數
import time
2 import sys
3 import os
4 import skip_read
5
6 def Find_small(r):
7
8
9 line=skip_read.process_file(r).strip()
10 # if line.startswith('-'):
11 # line=r.readline()
12 print line
13 value_smallest=int(line)
14 for line in r:
15 line=line.strip()
16 print line
17 if line!='-':
18 value=int(line)
19 else:
20 continue
21 value_smallest=min(value_smallest,value) if value<value_smallest: value_smallest=value
22
23 return value_smallest
24
25
26 if __name__=="__main__":
27
28 in_file=file(sys.argv[1],'r')
29 s=Find_small(in_file)
30 print 'Among them,the smallest value is:'
31 print s
32 in_file.close()
33 print time.clock()學習
Result:ip
$ python Find_smallest.py data.txt
123
35
46
687
24
14
45
456
6
Among them,the smallest value is:
6
0.078
數學
use min:it
$ python Find_smallest.py data.txt
123
35
46
687
24
14
45
456
6
Among them,the smallest value is:
6
0.046效率