Python Notes 4_ Dictionary Learning

Source: Internet
Author: User
Tags shallow copy

#dict: Dictionary with "{}" surrounded by "key: Value" Data collection#separated by "," between members#can contain any data type, including lists#unordered, data items can be changed#The key must be unique, and the value does not have to bedict1={'name':'SJL',' Age': 20}#view the class of an object, or the functionality that an object hasPrint(dir (dict1))#View details of object featuresPrint(Help (Type (DICT1)))#A dictionary of empty dictionaries and an element is establishedPrint('Empty Dictionary:',{})Print('Empty Dictionary:', Dict ())Print('Dictionary of an element:',{'a': 1})#accessing values in the dictionaryPrint('dict1["name"]:', dict1['name'])#gets the value specified in the dictionary forKeyinchDict1:Print('Key:', key)#default loop, output key value forKey,valueinchDict1.items ():Print('Key:value:', Key,':', value)#dictionary built-in functionsPrint('Len (dict1):', Len (Dict1))#calculates the number of dictionary elements, that is, the total number of keys. Print('Str (DICT1):', str (DICT1))#The output dictionary, expressed as a printable string. Print('type (DICT1):', type (DICT1))#returns the type of the variable entered and returns the dictionary type if the variable is a dictionary. #dictionary built-in methodsDict1.clear ()#Delete all the elements in the dictionary, no return value;Print('dict1.clear ():', Dict1) Dict1={'name':'SJL',' Age': 20,'Address':'Xian'}dict2=dict1.copy ();#returns a shallow copy of a dictionaryPrint('dict2=dict1.copy ():', dict2) Seq=('ZS','ls','ww') Dict3=dict.fromkeys (seq,10)#The Fromkeys () function is used to create a new dictionary, which is the key to the dictionary of elements in the sequence SEQ, and value is the initial value corresponding to all keys in the dictionaryPrint('Dict.fromkeys (seq,value):', str (DICT3))Print('Dict1.get (key):', Dict1.get ('name'))#returns the value of the specified key if the value does not return the default value in the dictionaryPrint('key in Dict:','name'inchDICT1)#returns False if the key returns true in the dictionary dictPrint('Dict1.keys ():', Dict1.keys ())#returns an array of traversed (key, value) tuples as a listPrint('Dict1.setdefault (key):', Dict1.setdefault ('name'))#similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to defaultPrint('Dict1.setdefault (key):', Dict1.setdefault ('Sex', None))Print("dict1.setdefault (' sex '):", Dict1) Dict1={'name':'SJL',' Age': 20,'Address':'Xian'}dict2={'Sex':'Girl'}dict1.update (DICT2)#update the key/value pairs of dictionary dict2 to Dict, no return valuePrint('dict1.update (dict2):', Dict1)Print('dict1.values ():', Dict1.values ())#returns all values in the dictionary as a listPrint('Dict1.pop (key):', Dict1.pop ('Sex'))#deletes the value of the dictionary given key key, and returns the value to be deleted. The key value must be given. Otherwise, the default value is returned. Print('Dict1.popitem ()', Dict1.popitem ())#randomly returns and deletes a pair of keys and values in the dictionary (typically delete the end pair). 

Python Notes 4_ Dictionary Learning

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.