我有 2 个名为x and list它们的定义如下:
x = torch.tensor(3)
list = torch.tensor([1,2,3,4,5])
现在我想获取元素的索引x from list。预期输出是一个整数:
2
我怎样才能轻松地做到这一点?
import torch
x = torch.tensor(3)
list = torch.tensor([1,2,3,4,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2]
list = torch.tensor([1,2,3,3,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2, 3]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)