【问题标题】:Python - multiple iterations over lists of different sizePython - 对不同大小的列表进行多次迭代
【发布时间】:2020-07-09 16:08:36
【问题描述】:

有人可以帮忙处理这段代码吗?我正在编写这段代码来计算一些方程的一些值。我通过将 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]

【问题讨论】:

  • 你能链接错误信息吗?
  • 还没有错误信息。我不能只是想办法解决这个问题。

标签: python data-science data-analysis


【解决方案1】:

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

很抱歉,您的问题很难理解。解释这一行。

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

编辑:

好的,我明白了。我认为最好的方法是使 P 成为二维数组。基本上,P 将是一个巨大的数组,其中包含多个数组。 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 个元素的数组。

:)

【讨论】:

  • 是的。但是,我相信它应该产生多个 P 数组(每个 dPdT 值一个)。我知道这不是应该解决的方式。
  • dPdT 中的每个值都应该像方程 P[i] = dPdT * listT[i] 中的常数
  • 谢谢!我试试看!
猜你喜欢
  • 1970-01-01
  • 2021-01-06
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 2019-11-10
  • 2016-08-28
相关资源
最近更新 更多