for循环数据,迭代和python中的一些统计计算

时间:2014-09-16 03:07:32

标签: python python-2.7 python-3.x

我是循环,它给了我能量值。我想知道如何在for循环中执行以下任务;

for i in range(1, 1000): 
    system = moves.move(system, 50, true)
    print("%d: %s" % (i, system.energies()))

1-如何将输出能量值存储在数组中或制作列表。例如[E1, E2, E3, E4, E5, ... E1000]

2-我想取存储数组[E991, E992 ... E1000]的最后10个值的平均值。我把它命名为计算平均值。

3-现在,程序测试计算出的平均值(从步骤2开始)和我在这样的脚本中设置的平均值

if calculated_mean <= set_mean 

然后程序继续下一步。

但如果条件不满足,则程序再运行1000步,从1000开始,从1000开始作为初始点。

3 个答案:

答案 0 :(得分:0)

注意:范围(1,1000)包含在左侧但不包含在右侧,因此您的最后一个值将是999,列表的长度将是999:

>>> range(1,1000)[-1]
999
>>> len(range(1,1000))
999

这是你问的怎么办?

def mean(vals):
    return (sum(vals)/len(vals))

set_mean = *your desired value*
calculated_mean = None
energy = []

while (calculated_mean is None) or (calculated_mean <= set_mean):
    for i in range(1, 1000):
        system = moves.move(system, 50, true)
        print("%d: %s" % (i, system.energies()))
        energy.append(system.energies())
    calculated_mean =  mean(energy[-10:])

答案 1 :(得分:0)

start = 0
calculated_mean = float('inf')
while calculated_mean > set_mean:
    energies = []
    for i in range(start, start + 1000): 
        system = moves.move(system, 50, true)
        energy = system.energies()
        energies.append(energy)
        print("%d: %s" % (i, energy))
    calculated_mean = sum(energies[-10:]) / 10

答案 2 :(得分:0)

我在计算calculated_mean时遇到错误。我认为这是因为我的输出值。我的输出能量看起来像这样,我对上一个术语感兴趣E_ {total} == 4.74348e + 08}

这里是3步96-97的样本输出;

95:{E_ {box1_inter} ^ {CLJ} == 4.73813e + 08,E_ {box1_inter} ^ {LJ} == 4.73813e + 08,E_ {box1_inter} ^ {coulomb} == 26.3989,E_ { box1_intraclj} ^ {CLJ} == 274847   E_ {box1_intraclj} ^ {LJ} == 24780,E_ {box1_intraclj} ^ {coulomb} == 250067,E_ {box1_intra} ^ {1-4 [LJ]} == 0,E_ {box1_intra} ^ {1-4 [coulomb]} == 0   E_ {box1_intra} ^ {1-4} == 0,E_ {box1_intra} ^ {angle} == 119360,E_ {box1_intra} ^ {bend-bend} == 0,E_ {box1_intra} ^ {bond} == 78389.5   E_ {box1_intra} ^ {dihedral} == 61627.6,E_ {box1_intra} ^ {improper} == 0.250546,E_ {box1_intra} ^ {internal} == 259378,E_ {box1_intra} ^ {stretch-bend-torsion} == 0   E_ {box1_intra} ^ {stretch-bend} == 0,E_ {box1_intra} ^ {stretch-stretch} == 0,E_ {box1_intra} ^ {urey-bradley} == 0,E_ {total} == 4.74348e +08}

96:{E_ {box1_inter} ^ {CLJ} == 4.73813e + 08,E_ {box1_inter} ^ {LJ} == 4.73813e + 08,E_ {box1_inter} ^ {coulomb} == 26.1315,E_ { box1_intraclj} ^ {CLJ} == 274847   E_ {box1_intraclj} ^ {LJ} == 24780,E_ {box1_intraclj} ^ {coulomb} == 250067,E_ {box1_intra} ^ {1-4 [LJ]} == 0,E_ {box1_intra} ^ {1-4 [coulomb]} == 0   E_ {box1_intra} ^ {1-4} == 0,E_ {box1_intra} ^ {angle} == 119360,E_ {box1_intra} ^ {bend-bend} == 0,E_ {box1_intra} ^ {bond} == 78389.5   E_ {box1_intra} ^ {dihedral} == 61627.6,E_ {box1_intra} ^ {improper} == 0.250546,E_ {box1_intra} ^ {internal} == 259378,E_ {box1_intra} ^ {stretch-bend-torsion} == 0   E_ {box1_intra} ^ {stretch-bend} == 0,E_ {box1_intra} ^ {stretch-stretch} == 0,E_ {box1_intra} ^ {urey-bradley} == 0,E_ {total} == 4.74348e +08}

97:{E_ {box1_inter} ^ {CLJ} == 4.73813e + 08,E_ {box1_inter} ^ {LJ} == 4.73813e + 08,E_ {box1_inter} ^ {coulomb} == 26.8793,E_ { box1_intraclj} ^ {CLJ} == 274847   E_ {box1_intraclj} ^ {LJ} == 24780,E_ {box1_intraclj} ^ {coulomb} == 250067,E_ {box1_intra} ^ {1-4 [LJ]} == 0,E_ {box1_intra} ^ {1-4 [coulomb]} == 0   E_ {box1_intra} ^ {1-4} == 0,E_ {box1_intra} ^ {angle} == 119360,E_ {box1_intra} ^ {bend-bend} == 0,E_ {box1_intra} ^ {bond} == 78389.5   E_ {box1_intra} ^ {dihedral} == 61627.6,E_ {box1_intra} ^ {improper} == 0.250546,E_ {box1_intra} ^ {internal} == 259378,E_ {box1_intra} ^ {stretch-bend-torsion} == 0   E_ {box1_intra} ^ {stretch-bend} == 0,E_ {box1_intra} ^ {stretch-stretch} == 0,E_ {box1_intra} ^ {urey-bradley} == 0,E_ {total} == 4.74348e +08}

任何线索?