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