多维Python数组的动态访问 [英] Dynamic Access of Multi dimensional Python Array

查看:56
本文介绍了多维Python数组的动态访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python新手.我对如何动态访问数组元素感到困惑.

I am a python newbie. I was confused on how to access array element dynamically.

我有一个列表b = [1,2,5,8]我可以动态获取,因此它的长度可以变化.借助此列表,我需要将多维数组更新为mArr [1] [2] [5] [8].列表的长度和数组维度匹配示例中的

I have a list b= [1,2,5,8] that I dynamically obtain so its length can vary. With help of this list I need to update multi-dimensional array as mArr[1] [2] [5] [8] . The length of the list and array dimension matches as given in the example

基本上,我正在寻找一种相对于列表"b"访问多维数组的技术,如:marr [b]的形式.这个m数组也是动态创建的.

Basically, I am looking a technique to access a multi dimensional array with respect to the list "b" as in the form of : marr[b]. This m-array is also dynamically created.

我尝试查看numpy的教程,但没有找到解决方案.我错过了什么吗?

I tried looking on to tutorials of numpy but was not figure out the solution.Am I missing something?

提前谢谢.

推荐答案

如果尺寸为[1,2,5,8],则可以为每个尺寸使用数字0, 0..1, 0..4, 0..7.

if the dimensions are [1,2,5,8] you can use numbers 0, 0..1, 0..4, 0..7 for each dimension.

Numpy使您可以使用元组访问职位:

Numpy lets you access positions with tuples:

shape = [1, 2, 5, 8]
pos = [0, 1, 1, 3]

my_array = np.ones(shape)
my_array[tuple(pos)] # will return 1

这篇关于多维Python数组的动态访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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