【问题标题】:Python, equating loop and arrayPython,等同循环和数组
【发布时间】:2014-01-20 03:11:10
【问题描述】:

我一直在试图弄清楚如何将从 while 循环中获得的数字关联到一个数组中。好久都想不通了。

我想使用循环将获得的 Vmax 值(在 500 到 600 之间以 20 为增量)放入一个数组中,然后再获得一个与 vmax 数组相关的另一个数组。

import pylab

print "vMax | Time to Land"
print "================================================================================="


sumx = 0
Vmax=500

while Vmax<=600:
if Vmax>=500 and Vmax<=600:
    avesp = pylab.zeros (6,float)
    avesp = ((Vmax + mins)/2.0) #creating formula for average speed
    t = pylab.zeros(6,float)
    t = dT / avesp #formula for time taken
    print Vmax
    sumx = sumx + Vmax
    Vmax = Vmax + 20
    vmax = pylab.array([Vmax],int)

【问题讨论】:

  • vmax = pylab.array([Vmax],int) 错字还是在未显示的代码中声明?

标签: python arrays loops matplotlib while-loop


【解决方案1】:

您可以使用列表推导来构建您的列表并应用 sum 函数来获得结果。 像这样的:

   result = [f2(r) for r in [f1(v) for v in range(500, 600, 20)]]
   sum_r = sum(result)

【讨论】:

    猜你喜欢
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2021-02-13
    相关资源
    最近更新 更多