gpt4 book ai didi

python - 为什么Python(3.7)不断覆盖 key :value pairs in my nested dictionary?

转载 作者:行者123 更新时间:2023-12-01 08:26:42 24 4
gpt4 key购买 nike

我想以此作为序言,说我通过编写以下内容在 python 终端中复制了代码的精神:

A = {}
A["Q"] = {}
A["Q"]["creation"] = {}
A["Q"]["creation"]["reactants_1"] = ["R1","R2"]

打印 A 给出了人们所期望的结果:

{"Q" : {"creation" : {"reactants_1" : ["R1","R2"]}}}

然后添加:

A["Q"]["creation"]["reactants_2"] = ["R3","R4"]

产量:

{"Q" : {"creation" : {"reactants_1" : ["R1","R2"], "reactants_2" : ["R3","R4"]}}}

但是,我在下面显示了一个函数,当正确运行时,它会分配前几个键:值对,但是当它循环到 x 的第二个值时,它会用新的键:值对替换之前写入的第一个键:值对关键是不同的。

使用上面的例子我们只会得到:

{"Q" : {"creation" : {"reactants_2" : ["R3","R4"]}}}

Reactions 是一个包含“e+e+p=e+H_1”之类的数组,格式如下:

["e+e+p=e+H_1", "e+e+p=e+H_2",...,"e+H_10=e+e+p"]

Species 是一个数组,例如:

[["e", 100000],["p", 100000],...]

现在我们只关心字符串部分。

diff_input 是一个空字典

reactions_const 包含,对于每个 react ,左侧和右侧分开 - 在函数早期被视为 [x][0][0] 和 [x][0][1] 以及一些其他信息目前并不重要。

rates_names 是每个 react 的唯一标识符数组,我稍后可以使用,因此使用字典方法。

打印语句都是我试图找出它不起作用的原因

def rate_eqns(diff_input, reactions, species, reactions_const, 
rates_names):

for x in range(len(reactions)):
# for each reaction
# split the left and right hand side up into lists of their
# respective species involved for counting later

print("reaction: " + str(x) + " " + str(reactions[x]))

species_lhs = reactions_const[x][0][0].split('+')
print("LHS = " + str(species_lhs))

species_rhs = reactions_const[x][0][1].split('+')

for y in range(len(species)):
# For each species, create a sub-dictionary
diff_input[species[y][0]] = {}

# create sub-dictionaries in each species for creation, destruction and preservation/neutral paths
diff_input[species[y][0]]["destruction"] = {}
diff_input[species[y][0]]["creation"] = {}
diff_input[species[y][0]]["balanced"] = {}

# check if species occurs in each reaction

if species[y][0] in reactions[x][0]:

# if you start with more of it than you finish its destruction
if species_lhs.count(species[y][0]) > species_rhs.count(species[y][0]):

# if true: add an entry to the dictionary which holds the reaction identifier
# bound to the destruction/creation/balanced identifier bound to the species identifier.
print("species:" + str(species[y][0]) + " found net loss from reactants")
print("LHS = " + str(species_lhs))
print("RHS = " + str(species_rhs))
print("reaction designation = " + str(rates_names[x]) + " Destruction of species")
print("-------------")
diff_input[species[y][0]]["destruction"][rates_names[x]] = species_lhs

elif species_lhs.count(species[y][0]) == species_rhs.count(species[y][0]):
print("species:" + str(species[y][0]) + " found no change in number")
print("LHS = " + str(species_lhs))
print("RHS = " + str(species_rhs))
print("reaction designation = " + str(rates_names[x]) + " preservation of species")
diff_input[species[y][0]]["balanced"][rates_names[x]] = species_lhs

elif species_lhs.count(species[y][0]) < species_rhs.count(species[y][0]):
print("species:" + str(species[y][0]) + " found net gain from reactants")
print("LHS = " + str(species_lhs))
print("RHS = " + str(species_rhs))
print("reaction designation = " + str(rates_names[x]) + " creation of species")
diff_input[species[y][0]]["creation"][rates_names[x]] = species_lhs

# else:
# print(str(species[y][0]) + " not found in reaction")
print(diff_input)
a = input("press return to continue")
with open('diff_input.txt', 'w') as file:
file.write(str(diff_input))

return diff_input

文件保存部分是可选的,是否有其他人遇到过用新键覆盖现有键的字典?

感谢您的耐心等待,并且感谢您对我的格式设置提出的任何建议(我试图使其尽可能好,而不包括脚本的其余部分)

最佳答案

species[1][0] 的实际值必须恰好等于 species[0][0],这样当它循环到x 的第二个值,赋值 diff_input[species[y][0]] = {} 将覆盖上一次迭代的子字典,因为 species [y][0] 保持不变。

更简单地说,使用示例代码,在外循环中进行以下初始化:

A["Q"] = {}
A["Q"]["creation"] = {}

所以即使你的内部循环为子字典分配了一些值:

A["Q"]["creation"]["reactants_1"] = ["R1","R2"]

A["Q"] = {} 只要 "Q" 继续作为作业的主键,就会在下一次迭代中覆盖它。

关于python - 为什么Python(3.7)不断覆盖 key :value pairs in my nested dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54190725/

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