In the process of doing the project, encountered a problem, the data saved to the dictionary, and later found that the data is not correct, the construction of the dictionary to troubleshoot the process is OK, and later suspected that the other part of the common use of this dictionary, to troubleshoot the code, found here should be problematic.
Score = Nonedeltascore = Result.get (' score ', [0 for _ in range (4)]if not score: score = deltascoreelse: for index In range (4): Score[index] + = Deltascore[index]
The code logic is to get the value of the score key from the dictionary, score to none, then directly assigns Deltascore to score, without notice is Dict's Get method, is to assign the memory of the key to Deltascore, is a list, Deltascore again assigns a share of memory to the score key in Score,score, Deltascore, Dict, and in the Else section, modifies score and modifies the value of Dict.
The main thing is to understand the data types of Python, as can be seen from the following validation code:
>>> A = {"test1":[1,2,3],"test2":[4,5,6]}>>>Improt Jsonfile"<stdin>", line1Improt JSON^syntaxerror:invalid Syntax>>> B = A.get ("test1", [0 for_inchRange3)])>>>Printb[1,2,3]>>> forIndexinchRange3): ... b[index]+=1...>>>>>>Printb[2,3,4]>>>Printa{'test1': [2,3,4],'test2': [4,5,6]}>>>
Python dictionary Get method