![](/img/trans.png)
[英]how to check if a float value is greater than another float value in 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.