具有多个 "if" 条件的双循环
Double loop with multiple "if" conditionals
我有两个数组 "a" 对应于我问题中的 "coordinates" 并且 b 对应于特定的坐标值。
我想知道 "a" 的行,其中我得到了 "b" 中的所有 3 个值,例如我想打印 [2,4,6] 的行,因为我有它们"b"
但是什么都没有出现...有一个错误...
import numpy as np
a = np.array([[1.,2.,3.],[4.,5.,6.],[2.,4.,6.]])
b = np.array([2,4,6,8,10])
for i in range(0,a.shape[0]):
for j in range(0,b.shape[0]):
if (b[j]==a[i,0] and b[j]==a[i,1]):
print i
试试这个:
for i in range(0,a.shape[0]):
if (a[i,0] in b and a[i,1] in b and a[i,2] in b):
print a[i]
因为您已经在使用 numpy
,这里应该也可以使用
import numpy as np
a[np.all(np.in1d(a,b).reshape(a.shape),axis=1)]
我有两个数组 "a" 对应于我问题中的 "coordinates" 并且 b 对应于特定的坐标值。 我想知道 "a" 的行,其中我得到了 "b" 中的所有 3 个值,例如我想打印 [2,4,6] 的行,因为我有它们"b" 但是什么都没有出现...有一个错误...
import numpy as np
a = np.array([[1.,2.,3.],[4.,5.,6.],[2.,4.,6.]])
b = np.array([2,4,6,8,10])
for i in range(0,a.shape[0]):
for j in range(0,b.shape[0]):
if (b[j]==a[i,0] and b[j]==a[i,1]):
print i
试试这个:
for i in range(0,a.shape[0]):
if (a[i,0] in b and a[i,1] in b and a[i,2] in b):
print a[i]
因为您已经在使用 numpy
,这里应该也可以使用
import numpy as np
a[np.all(np.in1d(a,b).reshape(a.shape),axis=1)]