Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用两种不同的条件在python中过滤数组_Python_Arrays_Numpy_Indexing - Fatal编程技术网

如何使用两种不同的条件在python中过滤数组

如何使用两种不同的条件在python中过滤数组,python,arrays,numpy,indexing,Python,Arrays,Numpy,Indexing,我正在尝试创建一个新数组,它将由数据值组成我不理解您的代码,但从标题中我建议您定义一个函数,给定一个点,如果该点应被过滤,则返回true。然后你可以做: def filter_point(point): # Conditions over a single point # You can read from global variables here, or create a partial function # to access external data

我正在尝试创建一个新数组,它将由数据值组成我不理解您的代码,但从标题中我建议您定义一个函数,给定一个点,如果该点应被过滤,则返回true。然后你可以做:

def filter_point(point):
    # Conditions over a single point
    # You can read from global variables here, or create a partial function
    # to access external data

    return condition1(point) and condition2(point)

result = filter(filter_point, list_of_points)
您不需要逻辑and。要打印数组的元组,只需打印以逗号分隔的数组序列:

print ID[hist[0]<20000], m[hist[1]<11.5]

你能准确地描述一下你想做什么吗?设置这两个条件并不能解释您想要实现的目标。理想情况下,您可以向我们展示一个2个小数组和所需输出的示例。我尝试只打印那些与邻居函数计算距离小于20000的行。同时,这些也必须满足m列的值小于11.5的条件,因此,为了澄清,您是想要两个数组的元组作为输出,通过布尔数组hist[0]过滤,还是想要ID[hist[0],我认为这就解决了问题。非常感谢您的时间
File "overlaping_halos_finder.py", line 53, in <module>
    combined = zip(ID[hist[0]<r_2[i] and m[hist[1]>1.e12]])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
print ID[hist[0]<20000] and m[hist[1]<11.5]
print ID[hist[0]<20000]
# ID M R X Y Z
6737.0 909913272632.0 103.06 1988.35 15894.6 40649.0
6617.0 997700063823.0 106.28 1523.55 15433.2 40363.2
6723.0 11 109.91 1993.05 15687.5 40557.2
def filter_point(point):
    # Conditions over a single point
    # You can read from global variables here, or create a partial function
    # to access external data

    return condition1(point) and condition2(point)

result = filter(filter_point, list_of_points)
print ID[hist[0]<20000], m[hist[1]<11.5]
res1, res2 = ID[hist[0]<20000], m[hist[1]<11.5]