• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

hbnn

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

一个很小很小的体会

关于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:

$ 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:

$ python Find_smallest.py data.txt
123
35
46
687
24
14
45
456
6
Among them,the smallest value is:
6
0.046

posted on 2014-01-17 14:45  hbnn  阅读(166)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3