繁体   English   中英

如何检查 integer 是否大于 python 中的某个值?

[英]How to check if an integer is greater than another by a certain value in python?

如何检查 y 中的整数是否比 x 大 2 或更多?

例如,如果我有这些变量并且只想返回 7 和 10:

x = 3
y = [1,3,4,7,10]

x = 3
y = [1,3,4,7,10]

out = [] # list to store values

# Make a loop
for v in y:
    if v - x >= 2: # check if greater than x by 2 or more
        out.append(v) # store value

你可以简单地做

x = 3
y = [1,3,4,7,10]
threshold = 2
print([ele for ele in y if (ele-x) >= threshold])

将 2 添加到x并与之比较。

x = 3
x_plus_2 = x + 2

result = [item for item in y if item >= x_plus_s]

您可以像这样使用 NumPy package:

import numpy as np
x = 3
# Convert y to NumPy array
y = np.array([1,3,4,7,10])
y[y >= x+2]

暂无
暂无

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

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