Chinaunix首页 | 论坛 | 博客
  • 博客访问: 58736
  • 博文数量: 14
  • 博客积分: 305
  • 博客等级: 二等列兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-14 17:18
文章分类

全部博文(14)

文章存档

2011年(14)

我的朋友

分类: Python/Ruby

2011-02-17 11:16:07

运行这个code可以得到距离某个值最近或最远的一个值
code如下:
例1:
  1. import math
  2. begins = 4
  3. dsg = 3.1, 31, 45, 1031
  4. cuts = [math.fabs(cs - begins) for cs in dsg]
  5. print max(cuts), min(cuts)
例2:
  1. import math
  2. begins = 4
  3. dsg = 3.1, 31, 45, 1031
  4. l = map(lambda x: math.fabs(x-begins), dsg)
  5. min(l), max(l)
例3:
  1. import math
  2. begins = 4
  3. dsg = 3.1, 31, 45, 1031
  4. a=[]
  5. for cs in dsg:
  6.    cuts = math.fabs(cs - begins)
  7.    a.append(cuts)
  8.    print cuts
  9. print min(a), max(a)

阅读(1962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~