【问题标题】:Python How to fill an array arr[i, j] within a for loop that calculates the values that will be put into i and jPython如何在for循环中填充数组arr [i,j],该循环计算将放入i和j中的值
【发布时间】:2016-10-18 08:22:12
【问题描述】:

这是一个必须计算达到特定密度分布所需的盐水流速的程序。我需要存储的重要变量是 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。您计算了Qt。这是针对 np.arrange(...) 中的每个值 z 完成的。你想让它看起来像什么。您的目标是生成如下内容:[ [ Q,t ], [2nd_Q, 2nd_t ], ... ]?你想用这些数据做什么?你说do I use two for loops here or can I use one?,但是为了什么?
  • 你确定你需要一个二维数组吗?
  • @rp.beltran :是的,这正是我需要的,[Q][t] 然后 [2nd_Q][2nd_t] 等等。我相信数组是最好的,但我是物理学家,对编程知之甚少。将这些数据存储在内存中的原因是,它将通过控制器馈送到我的水泵,然后每次 t 将它们的速度调整到 Q。感谢您的回答!
  • 虽然现在想起来,但它必须是一维数组

标签: python arrays


【解决方案1】:

如果我正确理解您的问题,这应该可以解决问题。

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] 值的列表。

【讨论】:

  • for item in arr: i = item[0] j = item[1] 这没有任何作用
  • 如果他需要用它们做计算那就更好了。但是,如果它们立即像这样使用,则将计算放入循环中会更有意义。
  • 我同意,但这更多是关于他如何提取存储在arr 中的数据的演示(因为他没有在他的问题中提供有关他计划如何使用它的详细信息)跨度>
【解决方案2】:

多维数组只是数组的数组。

如果arr 被定义为二维数组,看起来像:

[ [1, 2],
  [3, 4],
  [5, 6] ]

arr[0] 将返回 [1,2]

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

arr = []
arr.append([1,2])
arr.append([3,4])
arr.append([5,6])

在您的问题的上下文中,如果您只需要向数组中添加行以保存 Qt 两个计算值,请使用:

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

这样更高效,使用更少的代码,更容易设计。

希望对你有帮助。

【讨论】:

  • 太好了,谢谢!我不知道附加功能
【解决方案3】:

您可以使用broadcasting 创建一个没有 Python 循环的二维数组:

#Constants
a = 3
b = 2

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]
>>> 

另一个numpy broadcasting 链接。 那里还有很多其他很好的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多