简体   繁体   English

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

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

How could I check if the integers in y are greater than x by 2 or more?如何检查 y 中的整数是否比 x 大 2 或更多?

For example if I have these variables and want to only return 7 and 10:例如,如果我有这些变量并且只想返回 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

You can simply do你可以简单地做

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

Add 2 to x and compare with that.将 2 添加到x并与之比较。

x = 3
x_plus_2 = x + 2

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

You can use the NumPy package like this:您可以像这样使用 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.

相关问题 python - 如何检查浮点值是否大于python中的另一个浮点值? - how to check if a float value is greater than another float value in python? 检查 timedelta 是否大于 value (Python) - Check if timedelta is greater than value (Python) Python Sockets 将大于 127 的 integer 值作为单个字节发送 - Python Sockets send an integer value greater than 127 as a single byte 选择大于某个值的Python字典元素 - Selecting elements of a Python dictionary greater than a certain value 在 Python Selenium 中等待某个值大于等于某个数 - Waiting for a value to be greater than or equal to a certain number in Python Selenium 检查一个值是否大于另一个,以及是否不替换为较小的值idl到python - checking if a value is greater than another and if not replacing with the smaller value idl to python Python dataframe 检查列值是否大于前一列 - Python dataframe check if column value is greater than previous column Python. 如何查看当前值是否大于、等于或小于先前值? - Python. How do I check if the current value is greater, the same, or smaller than the previous? 如何找到大于某个余弦距离值的值对? - How to find pairs of values greater than a certain cosine distance value? 如何检查一个数字的每个数字是否大于或等于另一个数字? - How to check every digit of a number is greater or equal than another number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM