numpy函数将数组元素设置为给定索引列表的值 [英] numpy function to set elements of array to a value given a list of indices

查看:269
本文介绍了numpy函数将数组元素设置为给定索引列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个numpy函数,该函数将执行以下操作:

I'm looking for a numpy function that will do the equivalent of:

indices = set([1, 4, 5, 6, 7])
zero    = numpy.zeros(10)
for i in indices:
    zero[i] = 42

推荐答案

您可以给它一个索引列表:

You can just give it a list of indices:

indices = [1, 4, 5, 6, 7]
zero = numpy.zeros(10)
zero[indices] = 42

这篇关于numpy函数将数组元素设置为给定索引列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆