python同时满足两个条件_如何使用两个不同的条件过滤python中的数组?

我正在尝试创建一个新的数组,它将由第一列中的(数据值<=20000)和所有其他对应的列组成。

我正在做以下工作:从文本文件中读取数据。我正在寻找从最后一点到所有点的距离。然后,我将只取距离小于20000,M1小于11.5的行。数据如下:# ID M1 M2 M3 M4 R4 M5 R5 x y z

10217 11.467 11.502 13.428 13.599 432.17 13.266 281.06 34972.8 42985.9 14906

7991 11.529 11.559 13.438 13.520 435.23 13.224 272.23 8538.05 33219.8 43375.1

2100 11.526 11.573 13.478 13.490 448.97 13.356 301.27 9371.75 13734.1 43398.6

9467 11.557 11.621 13.481 13.537 449.99 13.367 303.67 33200.3 36008.9 12735.8

我的代码如下所示:import numpy as np

import matplotlib.pyplot as plt

halo = 'nntest.txt'

ID, m,r,x,y,z= np.loadtxt(halo, usecols=(0,6,7,8,9,10), unpack =True)

# selet the last point

m_mass = m[-1:]

ID_2 = ID[-1:]

r_2 = r[-1:]

x_2 = x[-1:]

y_2 = y[-1:]

z_2 = z[-1:]

#######################################

#Find distance to all points from our targeted point

nearest_neighbors = []

def neighbors(ID_2, cx,cy,cz, ID, m, r, x, y, z):

dist = np.sqrt((cx-x)**2 + (cy-y)**2 + (cz-z)**2)

return dist, ID, m, r, x, y, z

for i in range(len(ID_2)):

hist = neighbors(ID_2[i], m_2[i], r_2[i], x_2[i], y_2[i], z_2[i], ID, m , r, x, y, z)

#print all the IDs and all other data which are less than 20000 and M less than 11.5 of that targeted value

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

但我在设置这两个条件上有问题。它返回给我这个错误:File "overlaping_halos_finder.py", line 53, in

combined = zip(ID[hist[0]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

但是我想去掉前两个输出,只想打印第三行中的M列值<11.5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值