[英]How to fill an array in python with given list of values and list of indices?
What would be the simplest (fastest) way to take as input the following:将以下内容作为输入的最简单(最快)方法是什么:
M=np.zeros((2,5))
values=[a,b,c] #a,b and c are integers
indices=[(1,2,4),(0,2,4)]
and have as an output:并作为 output:
M=[[0,a,b,0,c],[a,0,b,0,c]]
try indexing尝试索引
M = np.zeros((2,5))
values = [7,8,9]
indices = np.array([[1,2,4],[0,2,4]])
M[np.arange(M.shape[0])[:,None],indices] = values
print(M)
[[0. 7. 8. 0. 9.]
[7. 0. 8. 0. 9.]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.