python列表访问得到的类型

第84行说我错了,'int' object is not subscriptable。这是为什么呀?应该如何解决?谢谢


import pandas as pd
import numpy as np
import math

df = pd.read_excel("pdata.xlsx")#读取资料

WM, WUM, WLM, WDM = 140, 20, 70, 50
W0, WU0, WL0, WD0 = 140, 20, 70, 50
B, C =0.3, 0.16
WMM = WM * (1 + B)
IM = 0.002
FC = 24
CG = 0.98
Kc = 0.97
FC = 10.0

df["P"] = df["和多布"] * 0.33 + df["马山"] * 0.14 + df["高潭"] * 0.33 + df["宝口"] * 0.20#算面平均降雨量
df["Ep"] = df["Eo"] * Kc#算蒸发能力
P = list(df["P"])
Ep = list(df["Ep"])
EU = []
EL = []
ED = []
E =[]
PE = []
a = []
R = []
WU = []
WL = []
WD = []
W = []

#计算第一天
#EU
if WU0 + P[0] >= Ep[0]:
    EU.append(Ep[0])
else:
    EU.append(WU0 + P[0])
#EL
if WU0 + P[0] >= Ep[0]:
    EL.append(0)
elif WU0 + P[0] < Ep[0] and WL0 >= C * WLM:
    EL.append((Ep[0] - EU[0]) * WL0 / WLM)
elif WU0 + P[0] < Ep[0] and WL0 < C * WLM and WL0 >= C * (Ep[0] - EU[0]):
    EL.append(C * (Ep[0] - EU[0]))
else:
    EL.append(WL0)
#ED
if WU0 + P[0] < Ep[0] and WL0 < C * (Ep[0] - EU[0]):
    ED.append(C * (Ep[0] - EU[0]) - EL[0])
else:
    ED.append(0)
#
E.append(EU[0] + EL[0] + ED[0])
PE.append(P[0] - E[0])

#计算R
if PE[0] > 0:
    a.append(WMM * (1 - pow((1 - W0/WM), (1 / (1 + B)))))
    if a[0] + PE[0] <= WMM:
        R.append(PE[0] - WM * pow((1 - a[0] / WMM), B + 1) + WM * pow((1 - (PE[0] + a[0]) / WMM), B + 1))
    else:
        R.append(PE[0] - (WM - W0))#这里写的W0
else:
    R.append(0)
    a[0] = 0
#计算WU
if WU0 + PE[0] - R[0] <= WUM:
    WU.append(WU0 + PE[0] - R[0])
else:
    WU.append(WUM)
#计算WL
if WU0 + WL0 + PE[0] - R[0] <= WUM + WLM:
    WL.append(WU0 + WL0 + PE[0] - R[0] - WUM)
else:
    WL.append(WLM)
#计算WD
if WU0 + WL0 + WD0 + PE[0] - R[0] <= WUM + WLM + WDM:
    WD.append(WU0 + WL0 + WD0 + PE[0] - R[0] - WUM - WLM)
else:
    WD.append(WDM)
#计算W
W.append(WU[0] + WL[0] + WM[0])
print(P)
print(Ep)
print(EU)
print(EL)
print(ED)
print(E)
print(PE)
print(a)
print(R)
print(WU)
print(WL)
print(WD)
print(W)


根据你的代码以及报错提示,代码中并没有定义列表‘WM’,‘WM’只是作为一个整型变量,但是在84行这里却将‘WM’作为列表来用,当然是有问题的
如果是列表就加个定义就好了;

如果是整型变量,可以按以下修改

W.append(WU[0] + WL[0] + WM)

希望能帮到你,加油~~

改为
W.append(WU[0] + WL[0] + WD[0])