gpt4 book ai didi

python - 从具有字典名称的列表中迭代字典

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:35 27 4
gpt4 key购买 nike

我正在尝试迭代列表,列表中的变量是字典名称,然后迭代这些字典中的键我的代码是这样的:

IA = {"V":"CVolt","T":"CTemp"}
CLR = {"V":"CLR_Volt","T":"CLR_Temp"}
GT = {"V":"GT_CVolt","T":"GT_Temp"}
PP = ["IA","GT","CLR"]
AFT = {"IA":0,"GT":0,"CLR":0}
AFV = {"IA":0,"GT":0,"CLR":0}
Voltage=0
Vs ={"IA":0.85,"GT":0.85,"CLR":0.85}
Tempreture = 0
Ts ={"IA":30,"GT":30,"CLR":30}
EAK = 7e3
Stress = {"IA":0,"GT":0,"CLR":0}
C = 1
Seff={"IA":0,"GT":0,"CLR":0}
csv.DictReader
with open(File,"r+") as Fin:
reader = csv.DictReader(Fin, dialect='excel')
for line in reader:
for i in PP:
AFT[i] = math.exp(EAK*((1/Ts[i])-(1/float(line[i["T"]]))))
AFV[i] = math.exp(float(line[i["T"]])-Vs[i])
Stress[i] = AFT[i] * AFV[i]
Seff[i] = Seff[i]+Stress[i]

我的问题是如何获取字典中的键值 i

AFT[i] = math.exp(EAK*((1/Ts[i])-(1/float(line[i["T"]]))))

在这种情况下,在第一个循环中我想得到 line["CTemp"]

欢迎任何建议或不同的方法

最佳答案

你可以使用Python内置的locals()功能:

>>> IA = {"V":"CVolt","T":"CTemp"}
>>> locals()['IA']
{'T': 'CTemp', 'V': 'CVolt'}
>>> locals()['IA']['T']
'CTemp'

或者更好,将变量放在一个大字典中,如下所示:

>>> vars = {
'IA': {"V":"CVolt","T":"CTemp"},
'CLR': {"V":"CLR_Volt","T":"CLR_Temp"},
...
}
>>> vars['IA']
{'T': 'CTemp', 'V': 'CVolt'}
>>> vars['IA']['T']
'CTemp'

感谢@damienfrancois 与本地人一起完成答案:

math.exp(EAK*((1/Ts[i])-(1/float(line[locals()[i]['T']]))))

使用变量字典:

math.exp(EAK*((1/Ts[i])-(1/float(line[vars[i]['T']]))))

关于python - 从具有字典名称的列表中迭代字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067054/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com