简体   繁体   English

比较列表中的变量

[英]Comparing variables in a list

I have a list of numbers:我有一个数字列表:

x = [601.4, 603.6, 600.5, 605.7]

and when a value in the list is within +1 of another one, I want to set a boolean variable to true.当列表中的一个值在另一个值的 +1 范围内时,我想将 boolean 变量设置为 true。

So this:所以这:

i = 0
while i < 4:
   if x[i] smart function here
      y = True
   else:
      y = False
   print (y)
   i = i + 1

would give out:会给出:

True
False
False
False

You could map the checking function f = lambda v: -1.0 <= v - x[i] <= 0.0 on the list x[0:i] + x[i+1:] that excludes the current index.你可以map检查 function f = lambda v: -1.0 <= v - x[i] <= 0.0在列表x[0:i] + x[i+1:] Finally, check the created iterable of the map result with any .最后,使用any检查 map 结果的创建迭代。

x = [601.4, 603.6, 600.5, 605.7]

i = 0
while i < 4:
    f = lambda v: -1.0 <= v - x[i] <= 0.0
    if any(map(f, x[0:i] + x[i+1:])):
        y = True
    else:
        y = False
    print (y)
    i = i + 1

 x = [601.4, 603.6, 600.5, 605.7] for value in x: y = False for val in x: if val:= value: if val < value < val + 1: y = True break print(y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM