Python社区  »  Python

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

Alvaro Uribe • 7 月前 • 75 次点击  

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]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54631
 
75 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 7 月前
Chris
Reply   •   1 楼
Chris    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]