在for循环中引用数组 [英] Referring to arrays in a for-loop

查看:76
本文介绍了在for循环中引用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有3个Numpy数组,其中包含以下名称的浮点数:一二三.

Suppose i have 3 Numpy arrays containing floats with the following names: one two three.

是否可以通过以下方式在for循环中引用它们:

Is it possible to refer to them in a for-loop in the following manner:

list=one two three

for arr in list:
    arr=arr[2,1]+1

上面的例子显然不起作用,但是我想知道是否有办法做到这一点?

The above example obviously does not work but i was wondering if there is a way to do this?

推荐答案

您需要指定保存值的位置:

You need to specify where you'd like to save the value:

arr[2,1] = arr[2,1] + 1

或者只是:

arr[2,1] += 1

因此整个代码变为:

import numpy as np
one = np.arange(6).reshape(3, 2)
two = np.arange(10).reshape(5, 2)
arrays = [one, two]
for arr in arrays:
    arr[2, 1] += 1

这篇关于在for循环中引用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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