繁体   English   中英

Python - 对不同大小的列表进行多次迭代

[英]Python - multiple iterations over lists of different size

有没有人可以帮助这个代码? 我正在编写这段代码来计算一些方程的一些值。 我通过将 CSV 值读取到字典中来解决问题,但是在计算这些值以达到最终的参数集之后,我找不到在同一个列表上重复迭代的方法。

简单来说,我有两个输入列表 dPdT 和 listT。 我需要在 listT 上迭代列表 dPdT 的每个参数并生成三个不同的列表 P。

我感谢任何愿意提供帮助的人。 这是一个课程的学习项目。

# Request user's interval parameters for calculations
print("Inform the temperature range and interval (°C). Only integers.")
minT = int(input("Min: "))
maxT = int(input("Max: "))
stepT = int(input("Temperature interval: "))

# create a list of temperature values to compute pressure parameters
listT = []

for x in range(minT, (maxT+stepT), stepT):
    listT.append(x)

# Open CSV file in read mode to acces data and read it into a dictionary
with open(CSVfile, "r") as CSV:
    reader = csv.DictReader(CSV)

    listDict = []

    # Creates a list of dictionaries with the fluid inclusion parameters
    for lines in reader:
        listDict.append(lines)

    # Define list parameters to be computated
    a, b, c, dPdT, P = [], [], [], [], []

    # Loop iterates over the dictionary list and computates parameters a,b,c stored in lists a,b,c
    for i, rows in enumerate(listDict):
        a.append(i)
        b.append(i)
        c.append(i)
        if "sal" in rows:
            a[i] = (18.28 + 1.4413 * float(rows["sal"]) + 0.0047241 * float(rows["sal"]) ** 2
                + 0.0024213 * float(rows["sal"]) ** 3 + 0.000038064 * float(rows["sal"]) ** 4)
            b[i] = (0.019041 - 1.4268 * 0.01 * float(rows["sal"]) + 5.66012 * 0.0001 * float(rows["sal"]) ** 2
                - 4.2329 * 0.000001 * float(rows["sal"]) ** 3 - 3.0354 * 0.00000001 * float(rows["sal"]) ** 4)
            c[i] = (- 1.5988 * 0.0001 + 3.6892 * (10 ** -5) * float(rows["sal"]) - 1.9473 * (10 ** -6) * float(rows["sal"]) ** 2
                + 4.1674 * (10 ** -8) * float(rows["sal"]) ** 3 - 3.3008 * (10 ** -10) * float(rows["sal"]) ** 4)

    # Loop iterates over the dictionary list and computates dPdT to store the values in list dPdT
    for i, rows in enumerate(listDict):
        dPdT.append(i)
        if "th" in rows:
            dPdT[i] = a[i] + b[i] * float(rows["th"])  + c[i] * float(rows["th"]) ** 2

    # Loop populates list P (pressure values)
    for i in range(len(listT)):
        P.append(i)
    
    # problem starts here: I need to iterate over the listT or P, repeating it for every dPdT values.
    # Loop to calculate P based on lits dPdT and listT. 
    while i in range(len(dPdT)):
        for j in range(len(P)):
            P[j] = dPdT[j] * listT[j]

我需要在 listT 上迭代列表 dPdT 的每个参数并生成三个不同的列表 P。

很抱歉很难理解你的问题。 解释这条线。

是否要将 dPdT 中的每个元素与 listT 中的每个元素相乘,从而得到长度为 = len(dPdT) * len(listT) 的 P 数组? 因为您当前的代码正是这样做的。

编辑:

好的,我明白了。 我认为 go 关于它的最佳方法是使 P 成为二维数组。 基本上,P 将是一个巨大的数组,其中包含多个 arrays。 len(P) = len(dPdT) 和 len(P[i]) = len(listT)

P = [ [0 for i in range(len(listT)] for j in range(len(dPdT)) ]
for row in range(len(dPdT)):
     for col in range(len(listT)):
          P[row][col] = dPdT[row] * listT[col]

我希望这是你想要的。 基本上,P 的第 n 个元素也将是包含 listT 中所有数字乘以 dPdT 的第 n 个元素的数组。

:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM