Classmate, your python~.

Source: Internet
Author: User

Do not know yesterday that popular science you did not understand, did not understand also does not matter, after all, now just learning basic grammar.

Today we are going to talk about the three methods of Copy,fromkeys,get.

Let's take a look at copy this method, copy this method is a bit special, we said in the previous copy of the method, as long as the copied value is modified, the copied value will also change, but, in the copy of this method here, if the substitution is implemented, then the copied value is not changed, However, if the deletion is implemented, the copied value will be changed. Let's take a look at the comparison code:

x = {'name':'Tom','Phone':['123','456']}y=x.deepcopy () y['name'] ='Pony'y['Phone'].remove ('123') y#press ENTER to get the result: {' name ': ' Pony ', ' phone ': [' 456 ']}x#press ENTER to get the result: {' name ': ' Tom ', ' phone ': [' 456 ']}

We see the above code, obviously, Y is a copy of x, and when we replace the value in Y, that is, to change Tom to pony, only Tom in Y will change, and Tom in X won't be changed. Then we take a look at the remove operation, and after remove, the contents of the X, y two dictionaries are changed. So, do we have any other way to prevent the copied values from being modified when the copy is modified? This time we need to introduce deepcopy this method, deepcopy use the same method as copy, but to add from copy import Deepcopy,deepcopy at the beginning does not modify the copied value.

Next we look at Fromkeys. This method is mainly used to create a dictionary with no special changes in the place, we look at the code is good:

{}.fromkeys (['name','age'])# Press ENTER , the result is: {' name ': None, ' Age ': none}

Is it basic to know how to use the code when you see it? Fromkeys parentheses are followed by the dictionary key, and the dictionary created with Fromkeys has no value.

Finally, let's take a look at get this method, the main function of this method is that when you query the dictionary does not have an item, the dictionary will not error, will output a none with you say no this item, let's look at the sample code:

x = {}Print(x['name'])#press ENTER to get the result: Traceback (most recent call last):#File "<stdin>", line 1, in <module>#keyerror: ' Name 'Print(X.get ('name','No'))#press ENTER to get the result is no

Look at the first code above, that is the hint of error, and then you look at the second piece of code, the result is no. This is what we have defined in the Get method to say that if the item is not found then the output is no. If you do not have a definition, the system defaults to output none.

OK, today's content is here!

(If the article image is infringing, please contact the author to delete)

Classmate, your python~.

Related Article

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.