为什么python会重写与未调用的字典键相关联的值?

2024-06-17 13:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我的脚本正在重写与键/尚未调用的键关联的值。你知道吗

我有一个嵌套字典,其中包含键的层次结构,如:

dict["species"]["type"]["reaction"]

这是另一本具有相同结构的词典(dict\ 0)的副本。副本将不断更新,原件保持不变以供参考。你知道吗

这些反应的值从小列表开始,比如从原始dict\u 0复制的[“e”,“e”,“p”],列出了物种参与的反应的左侧。你知道吗

例如:

dict_0["e"]["destruction"]["r_1"] = ["e", "e", "p"]
dict_0["p"]["destruction"]["r_1"] = ["e", "e", "p"]

因为e和p都参与了反应r\u 1,这是一个导致e和p净损失的过程

dict["e"]["destruction"]["r_1"] 

最初与dict\u 0中的值相同,但列表中的值会更新为表示该物种密度的浮点数,这些浮点数来自另一个名为densitions的字典,该字典简单地称为[species:density]作为key,value。你知道吗

当我在物种键的循环中循环反应键时,问题就出现了(将“type”键设置为“destruction”,因为这需要首先发生)。我检查反应键是否在已批准的反应标识符列表中,然后尝试将dict{}字典中该反应的值仅更改为densitions字典提供的densitions。这是通过使用相同的键来引用原始字典dict\u 0{},每次[“e”,“e”,“p”]列表总是这样,然后使用这些键作为密度字典的键来更新dict{}中的密度。这样你总能找到正确的钥匙。你知道吗

问题本身是,当我重写dict[“e”][“destruction”][reaction(例如“r\u 1”)]中包含的初始复制[“e”,“e”,“p”]到[density\u e,density\u e,density\u p]时,它也重写dict[“p”][destruction”][reaction(例如“r\u 1”)]的等价值

我写了大量的书面陈述,试图找出问题所在,但无法理解问题所在。我在控制台中尝试了一个简化的版本(非嵌套的),它的行为和预期的一样-我想可能是混淆了相同的值。我还可以手动更改dict[“e”][“destruction”][reaction(例如“r_1”)]中的值,而等效的['p']值现在不会更改。你知道吗

下面的代码并不是全部,但它是问题出现在wad中靠近末尾的某个地方,所有print语句都在这里。你知道吗

def dn_keys(diff_input,species_list,rates_names,densities_dict):

diff_input_new=copy.deepcopy(diff_input)
# Creates a new array from the master array and replaces the string values
# with floats using the dictionaries.
# print(diff_input_new)
# a = input("press to continue to density and rate population")
for species, value in diff_input_new.items():
    # for each species set of reactions:
    print("#####################")
    print("species = " + str(species))
    print("#####################")
    # a = input("press return to continue")
    # iterate over destructive processes first
    # key2 = "destruction"
    print("Destruction processes")
    print(str(diff_input_new[species]["destruction"]))
    for reaction, value in diff_input_new[species]["destruction"].items():
        if reaction in rates_names:

            print("Reaction found: " + str(reaction) +" for species: " + str(species))
            print("original entry: " + str(diff_input_new[species]["destruction"][reaction]))
            print(diff_input_new["e"]["destruction"][reaction])
            print(diff_input_new["p"]["destruction"][reaction])
            # for each species in the reaction:
            # print("array length = " + str(len(diff_input[species]["destruction"][reaction])))
            for i in range(len(diff_input[species]["destruction"][reaction])):
                # print(species)
                # if the rate hasn't been previously been added
                # find the densities and replace the dictionary entries appropriately
                print(i)   
                print("densities found for " + str(diff_input[species]["destruction"][reaction][i]) + ":") 
                print(densities_dict[diff_input[species]["destruction"][reaction][i]])
                print("--------")
                print(diff_input[species]["destruction"][reaction][i])
                print("in:")
                print(densities_dict.keys())
                print("---")
                print(diff_input_new[species]["destruction"][reaction][i])
                print("of")
                print(diff_input_new[species]["destruction"][reaction])
                diff_input_new[species]["destruction"][reaction][i] = densities_dict[diff_input[species]["destruction"][reaction][i]]
                print("converted to:")
                print(diff_input_new[species]["destruction"][reaction][i])
                print("---------------")
            diff_input_new["e"]["destruction"][reaction] = [1,1,1]

            print(diff_input_new["e"]["destruction"][reaction])
            print(diff_input_new["p"]["destruction"][reaction])

预期结果: dict[“e”][“destruction”][reaction]独立于dict[“p”][“destruction”][reaction]更新

实际结果: 两者一起更新。你知道吗

谢谢你的时间


Tags: theinnewforinput字典diffdensity
1条回答
网友
1楼 · 发布于 2024-06-17 13:45:41

这个问题正如BrunoDesthuillers所强调的那样,通过对python的名称-值关系的基本理解,BrunoDesthuillers加深了对这个问题的理解。你知道吗

在代码的早期,dictionary条目都声明为单个值的名称-一个名为species\u lhs的列表,因此当我更改一个实例时,它更改了所有的值。你知道吗

相关问题 更多 >