Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何在for循环中填充数组arr[i,j],该循环计算将放入i和j中的值_Python_Arrays - Fatal编程技术网

Python如何在for循环中填充数组arr[i,j],该循环计算将放入i和j中的值

Python如何在for循环中填充数组arr[i,j],该循环计算将放入i和j中的值,python,arrays,Python,Arrays,这是一个程序,必须计算达到一定密度剖面所需的盐水流速。我需要存储的重要变量是Q和t,因为它们将用于告诉我的泵实时运行的速度 import numpy as np for z in np.arange(0, 0.5, 0.01): Q = a + z # where a is a predefined number t = b + z # where b is a predefined number # here I would like to store

这是一个程序,必须计算达到一定密度剖面所需的盐水流速。我需要存储的重要变量是Q和t,因为它们将用于告诉我的泵实时运行的速度

import numpy as np

for z in np.arange(0, 0.5, 0.01):

    Q = a + z    # where a is a predefined number
    t = b + z    # where b is a predefined number

    # here I would like to store Q and t in an array

    for something in arr[i, j]:
        i = t
        j = Q

如果我没弄错你的问题,这就行了

arr = []
for z in np.arange(0, 0.5, 0.01):

    Q = a + z
    t = b + z

    # here I would like to store Q and t in an array
    arr.append([t,Q])

for item in arr:
    i = item[0]
    j = item[1]

它创建一个名为arr的列表,存储
[Q,t]
值的列表。

多维数组只是数组的数组

如果
arr
被定义为如下所示的2D数组:

[ [1, 2],
  [3, 4],
  [5, 6] ]
arr[0]
将返回
[1,2]

如果您有一个空数组,如
arr=[]
中所示,并且您想向其中添加一行,那么只需附加一个新数组即可。例如,要生成上面的二维数组,可以使用以下代码:

arr = []
arr.append([1,2])
arr.append([3,4])
arr.append([5,6])
在问题的上下文中,如果您只需要向数组中添加行来保存两个计算值
Q
t
,请使用:

import numpy as np

arr = []

for z in np.arange(0, 0.5, 0.01):

    Q = a + z # where a is a predefined number
    t = b + z # where b is a predefined number

    # here I would like to store Q and t in a 2D array
    arr.append([Q,t])

print arr
但是,如果可以在循环内完成计算,则根本不需要实例化任何数组

import numpy as np

for z in np.arange(0, 0.5, 0.01):

    Q = a + z # where a is a predefined number
    t = b + z # where b is a predefined number

    # Do your calculations with Q ant t
    print Q + t # replace with your actual calculations
这更高效,使用更少的代码,更易于设计


我希望这会有所帮助。

您可以使用以下方法创建无Python循环的二维数组:

制作
z
二维阵列

z = np.array((np.arange(0, 0.1, 0.01),np.arange(0, 0.1, 0.01)))

>>> print(z)
[[ 0.    0.01  0.02  0.03  0.04  0.05  0.06  0.07  0.08  0.09]
 [ 0.    0.01  0.02  0.03  0.04  0.05  0.06  0.07  0.08  0.09]]
>>> 
将常量放入一维数组中

c = np.array((a,b))

>>> print(c)
[3 2]
>>> print(z.shape, z.T.shape, c.shape)
((2, 10), (10, 2), (2,))
>>>
将常数添加到z

qt = z.T + c

>>> print(qt)
[[ 3.    2.  ]
 [ 3.01  2.01]
 [ 3.02  2.02]
 [ 3.03  2.03]
 [ 3.04  2.04]
 [ 3.05  2.05]
 [ 3.06  2.06]
 [ 3.07  2.07]
 [ 3.08  2.08]
 [ 3.09  2.09]]
>>>

for thing in qt:
    print(thing)

>>> 
[ 3.  2.]
[ 3.01  2.01]
[ 3.02  2.02]
[ 3.03  2.03]
[ 3.04  2.04]
[ 3.05  2.05]
[ 3.06  2.06]
[ 3.07  2.07]
[ 3.08  2.08]
[ 3.09  2.09]
>>> 


另一个链接。还有许多其他很好的解释。

这个问题需要澄清。您有一个2D数组
arr
。您计算了
Q
t
。这是对np.arrange(…)中的每个值
z
执行的。你想让它看起来像什么。您的目标是生成类似:[[Q,t],[2nd_Q,2nd_t],…]的内容吗?你想用这些数据做什么?你说
我在这里使用两个循环还是一个循环?
,但是用于什么?你确定2D数组就是你需要的吗?@rp.beltran:是的,这正是我需要的,[Q][t]然后[2nd_Q][2nd_t]等等。我相信数组是最好的,但我是物理学家,对编程知之甚少。将这些数据存储在内存中的原因是,这些数据将通过一个控制器输送到我的水泵上,然后每次t时,水泵将把它们的速度调整到Q。谢谢你的回答!尽管现在正在考虑,但对于arr中的项,它只能是一个1D数组
:i=item[0]j=item[1]
,如果他需要使用它们进行计算,这并不能做任何事情。但是,如果他们立即像这样使用,那么将计算放在循环中就更有意义了。我同意,但这更像是一个演示,演示了他如何提取存储在
arr
中的数据(因为他在问题中没有提供关于他打算如何使用它的详细信息),非常感谢!我不知道append函数
qt = z.T + c

>>> print(qt)
[[ 3.    2.  ]
 [ 3.01  2.01]
 [ 3.02  2.02]
 [ 3.03  2.03]
 [ 3.04  2.04]
 [ 3.05  2.05]
 [ 3.06  2.06]
 [ 3.07  2.07]
 [ 3.08  2.08]
 [ 3.09  2.09]]
>>>

for thing in qt:
    print(thing)

>>> 
[ 3.  2.]
[ 3.01  2.01]
[ 3.02  2.02]
[ 3.03  2.03]
[ 3.04  2.04]
[ 3.05  2.05]
[ 3.06  2.06]
[ 3.07  2.07]
[ 3.08  2.08]
[ 3.09  2.09]
>>>