python从布尔列掩码创建数组子集
我需要创建一个函数或方程来转换这个输入python从布尔列掩码创建数组子集,python,numpy,Python,Numpy,我需要创建一个函数或方程来转换这个输入 a = [True, False, True] b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 在这个输出中 c = [[1, 3], [4, 6], [7, 9]] 注:以上阵列均为numpy阵列。谢谢 您可以使用itertools压缩 from itertools import compress c = [list(compress(i, a)) for i in b] [[1, 3], [4, 6], [7, 9]]
a = [True, False, True]
b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
在这个输出中
c = [[1, 3], [4, 6], [7, 9]]
注:以上阵列均为numpy阵列。谢谢 您可以使用itertools压缩
from itertools import compress
c = [list(compress(i, a)) for i in b]
[[1, 3], [4, 6], [7, 9]]
您可以使用itertools压缩
from itertools import compress
c = [list(compress(i, a)) for i in b]
[[1, 3], [4, 6], [7, 9]]
numpy
解决方案
import numpy as np
a = np.array([True, False, True])
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b[:,a]
array([[1, 3],
[4, 6],
[7, 9]])
numpy
解决方案
import numpy as np
a = np.array([True, False, True])
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b[:,a]
array([[1, 3],
[4, 6],
[7, 9]])
然后添加
numpy
tag类似[[e代表x,e在zip(a,子列表)中,如果x]代表b中的子列表]
会起作用,但我怀疑有更有效的numpy解决方案,然后添加numpy
tag类似[[e代表x,e在zip(a,子列表)中,如果x]代表b中的子列表]
会起作用,但我怀疑有一种更有效的numpy解决方案NICE,更有意义+1 NICE,更有意义+1