蟒蛇/ numpy的:值设置为索引范围 [英] Python/Numpy: Setting values to index ranges

查看:201
本文介绍了蟒蛇/ numpy的:值设置为索引范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我可以创建这样一个5维数组:

Using Numpy, I can create a 5-dimensional array like this:

>>> faces = numpy.zeros((3, 3, 3, 6, 3))

我想(所有索引,所有的索引,0,4)设置为(1,1,1)。这是可能只使用numpy的(不Python的循环)?

I want (all indexes, all indexes, 0, 4) to be set to (1., 1., 1.). Is this possible by only using Numpy (no Python loops)?

推荐答案

以下两项将做到这一点:

Both of the following will do it:

faces[:,:,0,4] = 1

faces[:,:,0,4] = (1, 1, 1)

第一种使用一个事实,即所有三个值是由具有numpy的播出1到正确的尺寸相同。

The first uses the fact that all three values are the same by having NumPy broadcast the 1 to the correct dimensions.

二是在更普遍一点可以分配不同的值的三要素。

The second is a little bit more general in that you can assign different values to the three elements.

这篇关于蟒蛇/ numpy的:值设置为索引范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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