1 回答

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
手動(dòng)將其轉(zhuǎn)換為 int 元組可以實(shí)現(xiàn)您所尋求的索引。測(cè)試下面的代碼。
import numpy as np
# your example had d=3, but you may have something else.
# this is just creation of some random data...
d=3
answers = np.random.normal(size=[2] * d)
truth = tuple(np.random.choice([True, False], replace=True, size=d))
# manual conversion to int-tuple is needed, for numpy to understand that it
# is a indexing tuple.
# the box at the top of https://numpy.org/doc/stable/reference/arrays.indexing.html
# says that tuple indexing is just like normal indexing, and in that case, we
# need to have normal ints.
idx = tuple(int(b) for b in truth)
answer = answers[idx]
print(answer)
添加回答
舉報(bào)