有一个布尔条件列表列表我想生成一个矩阵,其中包含具有False或True值的列表
对于这个例子
values = [[True, False], [False], [True], [True, False]]
结果将是
False True
0 1 1
1 1 0
2 0 1
3 1 1
我试着这样做:
nodes = [True, False]
values = [[True, False], [False], [True], [True, False]]
res = np.array([[int(cond in vals) for vals in values] for cond in nodes],
dtype=[(node, int) for node in nodes])
但我收到错误TypeError: data type not understood
答案 0 :(得分:2)
尝试dtype=int
,然后我们
import numpy as np
nodes = [True, False]
values = [[True, False], [False], [True], [True, False]]
res = np.array([[cond in vals for vals in values] for cond in nodes], dtype=int)
print(res)
# Output
[[1 0 1 1]
[1 1 0 1]]