【发布时间】:2017-09-08 23:33:12
【问题描述】:
有没有办法在没有循环的情况下得到这个结果?我曾多次尝试使用 W[range(W.shape[0]),... 进行花式索引,但到目前为止都没有成功。
import itertools
import numpy as np
n = 4
ct = 2
one_index_tuples = list(itertools.combinations(range(n), r=ct))
W = np.zeros((len(one_index_tuples), n), dtype='int')
for row_index, col_index in enumerate(one_index_tuples):
W[row_index, col_index] = 1
print(W)
结果:
[[1 1 0 0]
[1 0 1 0]
[1 0 0 1]
[0 1 1 0]
[0 1 0 1]
[0 0 1 1]]
【问题讨论】: