我需要在一个空的python列表中替换操作的结果

时间:2019-02-14 02:46:52

标签: python

对不起,我是新手,我需要T的9个值,替换T2列表中的每个值。我有这段python代码。

import numpy as np
Nb=10
Bx = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
lx= np.array([ 25.5,  50.2,  80.3, 101.6, 130.4, 165.8, 190,  235,  237.9, 
300 ])
visc = np.array([1135.7891419, 1135.7891419, 1135.7891419, 1135.7891419, 
1135.7891419, 1135.7891419, 1135.7891419, 1135.7891419, 1135.7891419, 
1135.7891419])
y=300
z=100 
K= np.array([700, 750, 735, 780, 770, 775, 776, 778, 790, 792])
for i in range(Nb):
    lista = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    if i <Nb-1:
       T= (1/(visc[i]*Bx[i]))*(2*0.001127/(((lx[i])/(y*z*K[i+1]))+((lx[i+1])/(y*z*K[i+1]))))
       lista.insert(i, T)
    else:
       T=0
       lista.insert(i, T)
       break
    i=i+1
    print ("Transmisibilidad", i, "+1/2=", T)
print (lista)

但是,没有将T的值添加到T2列表中,结果是这样。

TRANSMISIBILIDADES
Transmisibilidad 1 +1/2= 0.589851703213957
Transmisibilidad 2 +1/2= 0.33531600348375945
Transmisibilidad 3 +1/2= 0.2552932649292381
Transmisibilidad 4 +1/2= 0.19759693062435824
Transmisibilidad 5 +1/2= 0.155773688040985
Transmisibilidad 6 +1/2= 0.12984739206384902
Transmisibilidad 7 +1/2= 0.10898534939640692
Transmisibilidad 8 +1/2= 0.0994569716143068
Transmisibilidad 9 +1/2= 0.08765992428622633
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

我需要这样的东西,感谢您的帮助。

Transmisibilidad 1 +1/2= 0.589851703213957
Transmisibilidad 2 +1/2= 0.33531600348375945
Transmisibilidad 3 +1/2= 0.2552932649292381
Transmisibilidad 4 +1/2= 0.19759693062435824
Transmisibilidad 5 +1/2= 0.155773688040985
Transmisibilidad 6 +1/2= 0.12984739206384902
Transmisibilidad 7 +1/2= 0.10898534939640692
Transmisibilidad 8 +1/2= 0.0994569716143068
Transmisibilidad 9 +1/2= 0.08765992428622633
[0.589851703213957, 0.33531600348375945, 0.2552932649292381, 0.19759693062435824, 0.155773688040985, 0.12984739206384902, 0.10898534939640692, 0.0994569716143068, 0.08765992428622633, 0]

1 个答案:

答案 0 :(得分:1)

首先,lista在每次迭代中都会重新分配。

第二,list.insert不会替换元素。它会在列表中插入一个新元素,从而使列表更长

解决上述问题(注意:我删除了所有∆s):

Nb=10
Bx = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
visc = np.array([1135.7891419, 1135.7891419, 1135.7891419, 1135.7891419, 
1135.7891419, 1135.7891419, 1135.7891419, 1135.7891419, 1135.7891419, 
1135.7891419])
lx= np.array([ 25.5,  50.2,  80.3, 101.6, 130.4, 165.8, 190,  235,  237.9, 300 ])
y=300
z=100 
K= np.array([700, 750, 735, 780, 770, 775, 776, 778, 790, 792])
lista = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
# Brought to outside of the loop

for i in range(Nb):
    if i <Nb-1:
       T= (1/(visc[i]*Bx[i]))*(2*0.001127/(((lx[i])/(y*z*K[i+1]))+((lx[i+1])/(y*z*K[i+1]))))
       lista[i] = T
    else:
       T=0
       lista[i] = T
       # break << Unnecessary
    i=i+1
    print ("Transmisibilidad", i, "+1/2=", T)
print (lista)

Transmisibilidad 1 +1/2= 0.5898517032138213
Transmisibilidad 2 +1/2= 0.3353160034836823
Transmisibilidad 3 +1/2= 0.25529326492917936
Transmisibilidad 4 +1/2= 0.1975969306243128
Transmisibilidad 5 +1/2= 0.15577368804094915
Transmisibilidad 6 +1/2= 0.12984739206381915
Transmisibilidad 7 +1/2= 0.10898534939638185
Transmisibilidad 8 +1/2= 0.09945697161428392
Transmisibilidad 9 +1/2= 0.08765992428620617
[0.5898517032138213, 0.3353160034836823, 0.25529326492917936, 0.1975969306243128, 0.15577368804094915, 0.12984739206381915, 0.10898534939638185, 0.09945697161428392, 0.08765992428620617, 0]