"Python Tour" chapter II (IV): dictionary

Source: Internet
Author: User

Description

It is obvious that the learning process of a dictionary in Python is the same as a list, mainly focusing on the following functions:

>>> xpleaf.xpleaf.clear (Xpleaf.copy (Xpleaf.get (Xpleaf.has_key (Xpleaf.items (Xpleaf.keys (Xpleaf.pop ( Xpleaf.popitem (Xpleaf.setdefault (Xpleaf.update (



1. Basic operation


--Create a dictionary

>>> xpleaf = {...         ' Name ': ' Xpleaf ',...         ' Occupation ': ' Student ',...         ' Hobby ': ' Computer ',... ' Dream ': ' Excellent Hacker ' ...} >>> xpleaf{' hobby ': ' Computer ', ' dream ': ' Excellent hacker ', ' name ': ' Xpleaf ', ' occupation ': ' Student '}

• It is easy to observe that the output of the dictionary is not output in the order in which the dictionary was created, because the dictionary finds content by hash value instead of by index number;

· {Key:value} is the basic syntax format for a dictionary, key is unique, and value can be used for most data types;


--See what the key values correspond to

>>> xpleaf[' hobby ' computer '


--Modify the contents of the key values

>>> xpleaf[' hobby ' = ' it ' >>> xpleaf{' hobby ': ' It ', ' dream ': ' Excellent hacker ', ' name ': ' Xpleaf ', ' OCC Upation ': ' Student '}


--Add a key-value pair

>>> xpleaf[' girlfriend ' = ' none ' >>> xpleaf{' girlfriend ': ' None ', ' hobby ': ' IT ', ' dream ': ' Excellent Hacker ', ' name ': ' Xpleaf ', ' occupation ': ' Student '}

• The order of the added elements in the dictionary is random because the index number does not make sense for the dictionary (search for value values by hash value);



2.has_key () function


• Function: Accepts the key query, returns the query dictionary in the form of the bool value to have the key;


• The demo is as follows:

>>> Xpleaf.has_key (' dream ') true>>> Xpleaf.has_key (' wife ') False



3.items () function


• Function: Converts a dictionary to a list of elements that are tuples, where the left element is key and the right element is value;


• The demo is as follows:

>>> xpleaf.items (' girlfriend ', ' none '), (' Hobby ', ' IT '), (' Dream ', ' excellent Hacker '), (' Name ', ' Xpleaf '), (' occupation ', ' student ')]

• Based on the above output format, the dictionary key and value can be traversed, as follows:

>>> for Key,value in Xpleaf.items (): ... print key,value ... girlfriend nonehobby itdream excellent hackername xp Leafoccupation Student

The principle of the item () function is that converting a dictionary to a list is stored in memory, which is slower in the case of large data volumes;

• Dictionary traversal methods for large data volumes:

>>> for key in Xpleaf: ... print Key,xpleaf[key] ... girlfriend nonehobby Itdream excellent hackername XPLEAFOCCU Pation Student



4.get () function


• Function: Take the value of the corresponding key;


• The demo is as follows:

>>> xpleaf{' girlfriend ': ' None ', ' hobby ': ' IT ', ' dream ': ' Excellent hacker ', ' name ': ' Xpleaf ', ' occupation ': ' St Udent '}>>> xpleaf.get (' dream ') ' excellent hacker ' >>> xpleaf.get (' wife ') ===> without the key value there will be no output >>>

• The method that is equivalent to Dict[key] takes value;



5.keys () function


• Function: Remove the key value from the dictionary and generate the corresponding list;


• The demo is as follows:

>>> Xpleaf.keys () [' Girlfriend ', ' hobby ', ' dream ', ' name ', ' occupation ']



5.pop () function


• Function: Eject a key, that is, delete a value pair;


• The demo is as follows:

>>> xpleaf{' girlfriend ': ' None ', ' hobby ': ' IT ', ' dream ': ' Excellent hacker ', ' name ': ' Xpleaf ', ' occupation ': ' St Udent '}>>> xpleaf.pop (' girlfriend ') ' None ' >>> xpleaf{' hobby ': ' IT ', ' dream ': ' Excellent hacker ', ' Name ': ' Xpleaf ', ' occupation ': ' Student '}



6.popitem () function


• Function: Delete the elements in the dictionary sequentially;


• The demo is as follows:

>>> a{' A ': 1, ' C ': 3, ' B ': 2, ' E ': 5, ' d ': 4, 6: ' F '}>>> a.popitem (' A ', 1) >>> A.popitem () (' C ') , 3) >>> A.popitem (' B ', 2) >>> A.popitem (' E ', 5) >>> A.popitem () (' d ', 4) >>> A.popitem () (6, ' F ')



7.setdefault () function


• Add an element to the dictionary, and do not make any changes if the element is originally present;


• The demo is as follows:

>>> xpleaf{' hobby ':  ' IT ',  ' dream ':  ' excellent hacker ',  ' name ':  ' Xpleaf ',  ' occupation ':  ' student '}>>> xpleaf.setdefault (' hobby ', ' computer ')      ===> ' hobby ' key value pair already exists and will not add ' it ' >>> xpleaf{' hobby ':  ' it ',  ' dream ':  ' Excellent hacker ',  ' name ':  ' xpleaf ',  ' occupation ':  ' student '}>>>  Xpleaf.setdefault (' weight ', ' 55kg ')     ===> ' weight ' key value pair does not exist and will be added ' 55kg ' >>>  xpleaf{' name ':  ' xpleaf ',  ' weight ':  ' 55kg ',  ' hobby ':  ' IT ',  ' dream ':  ' Excellent hacker ',  ' occupation ':  ' student '}>>> xpleaf.setdefault (' wife ')      ===> add no key-value pairs, >>> xpleaf{' name ':  ' xpleaf ',  ' weight ':  ' 55kg ',   ' wife ': none,  ' hobby ':  ' IT ',  ' dream ':  ' excellent hacker ',  ' occupation ':  ' student '}



8.update () function


• Function: Merge two dictionaries


• The demo is as follows:

>>> a{' A ': 1, ' C ': 3, ' B ': 2}>>> b{' E ': 4, ' G ': 6, ' F ': 5}>>> a.update (b) >>> a{' A ': 1, ' C ': 3, ' B ': 2, ' E ': 4, ' G ': 6, ' F ': 5}>>> b{' E ': 4, ' G ': 6, ' F ': 5}

• The order of merging is still random and the principle is the same as before;

• Only dictionary A is updated, and the dictionary B does not change;

• If you merge a dictionary with duplicate item items, you will overwrite them:

>>> a{' A ': 1, ' C ': 3, ' B ': 2, ' E ': 4, ' G ': 6, ' F ': 5}>>> c{' B ': ' Cover2 ', ' g ': ' Cover1 '}>>> a.u Pdate (c) >>> a{' A ': 1, ' C ': 3, ' B ': ' Cover2 ', ' E ': 4, ' G ': ' Cover1 ', ' F ': 5}



9.values () function


• Function: Take the value of all keys in the dictionary and generate the corresponding list


• The demo is as follows:

>>> xpleaf{' name ': ' Xpleaf ', ' weight ': ' 55kg ', ' wife ': None, ' hobby ': ' IT ', ' dream ': ' Excellent hacker ', ' Occupat Ion ': ' Student '}>>> xpleaf.values () [' Xpleaf ', ' 55kg ', None, ' IT ', ' excellent Hacker ', ' student ']

• Used in dictionaries with the same data types as value values for batch analysis of data;



10.clear () function


• Function: Empty the item of the dictionary


• The demo is as follows:

>>> a{' A ': 1, ' C ': 3, ' B ': ' Cover2 ', ' E ': 4, ' G ': ' Cover1 ', ' F ': 5}>>> a.clear () >>> a{}

• Unlike Del, Del is a direct delete dictionary:

>>> del a>>> Atraceback (most recent): File ' <stdin> ', line 1, in <module>nameerr Or:name ' A ' is not defined



11.copy () function


• Function: A shallow copy of the dictionary;


· "Copy" in Python under normal circumstances:

>>> xpleaf{' name ':  ' xpleaf ',  ' weight ':  ' 55kg ',  ' wife ': none,  ' Hobby ':  ' IT ',  ' dream ':  ' excellent hacker ',  ' occupation ':  ' student '}>>>  xpleaf_copy = xpleaf>>> xpleaf_copy{' name ':  ' xpleaf ',  ' weight ':  ' 55kg ',  ' wife ': none,  ' hobby ':  ' IT ',  ' dream ':  ' excellent hacker ',  ' Occupation ':  ' student '}>>> xpleaf[' hobby '] =  ' It_field ' >>> xpleaf_ copy{' name ':  ' xpleaf ',  ' weight ':  ' 55kg ',  ' wife ': none,  ' hobby ':  ' It_field ',   ' dream ':  ' excellent hacker ',  ' occupation ':  ' student '}>>> xpleaf_copy[' Wife '] =  ' None!!! ' >>> xpleaf_copy{' name ':  ' xpleaf ',  ' weight ':  ' 55kg ',  ' wife ':  ' None!!! ',   ' hobby ':  ' It_field ',  ' dream ':  ' excellent hacker ',  ' occupation ':  ' stUdent '}>>> xpleaf{' name ':  ' xpleaf ',  ' weight ':  ' 55kg ',  ' wife ':  ' None!!! ' ,  ' hobby ':  ' It_field ',  ' dream ':  ' excellent hacker ',  ' occupation ':  ' student ' }

• Assigning variables to other variables simply passes the object (the actual dictionary) as a reference, and modifying any of the references will change the value of the original object;

The shallow copy function of copy () is not a reference pass:

>>> xpleaf_copy2 = xpleaf.copy () >>> xpleaf_copy2{' name ': ' Xpleaf ', ' weight ': ' 55kg ', ' wife ': ' None!!! ' , ' hobby ': ' It_field ', ' dream ': ' Excellent hacker ', ' occupation ': ' Student '}>>> xpleaf_copy2[' wife '] = ' CL ' >  >> xpleaf_copy2{' name ': ' Xpleaf ', ' weight ': ' 55kg ', ' wife ': ' CL ', ' hobby ': ' It_field ', ' dream ': ' Excellent hacker ', ' Occupation ': ' Student '}>>> xpleaf{' name ': ' Xpleaf ', ' weight ': ' 55kg ', ' wife ': ' None!!! ', ' hobby ': ' It_field ', ' Dream ': ' Excellent hacker ', ' occupation ': ' Student '}

• Of course the more important role of copy () is not only in this, but here simply refers to its role.



This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1693955

"Python Tour" chapter II (IV): dictionary

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.