Python dictionary Get method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.