Dict of Python data types

Source: Internet
Author: User

1. Clear: Delete all elements

#D. Clear (), None.   from ddic_a   ={1:'kong',2:'zha',3: ' Gen ' }dic_a.clear () print (dic_a) result: {}

2, Fromkeys (): Build a new dictionary from the Key,value of the sequence key and value generation dictionary

#dict.  Fromkeys(seq[, value]))      
SEQ: It's for the dictionary key.
Value: is the default value of the dictionary

Seq = ('Name',' Age','Sex') New_dic= Dict.fromkeys (seq,Ten) print (new_dic) Result: {' Age':Ten,'Sex':Ten,'Name':Ten}

3. Get: Get dictionary values

#Get(self, k, d=None)
= {1:'kong',2:'zha'}print (dic. Get (1,10)) Result:'kong'

4. Items: Returns a collection object of a class

DIC = {1:'kong',2:'zha'}print ( Dic.items ()) Result: Dict_items ([1'kong'), (2' Zha ')])

5. Keys: Returns a collection object of a class

DIC = {1:'kong',2:'zha'=  Dic.keys () print (new_dic) for in new_dic:    print (x) Results: Dict_keys ([1  2])12

6. Pop: Delete the key value specified by the dictionary, return a value, you must specify the key to delete

return= {1:'kong',2:'zha' }print (Dic.pop (1)) print (DIC) Result: kong{2'Zha '}

7, Popitem: Random removal of the dictionary key-value pairs, return a tuple, if the dictionary is empty error

return  as a         2 if  is  = {1:'kong',2:'zha' ,3:'gen'}print (Dic.popitem ()) print (DIC)
Results:

(1, ' Kong ')
{2: ' Zha ', 3: ' Gen '}

8. SetDefault: If the key is in the dictionary, the value corresponding to the key is returned, if the key is not in the dictionary, the key value is inserted into the dictionary.

#D. SetDefault (K[,d]), D.Get(K,d), alsoSetD[k]=difK NotinchDdic= {1:'Kong',2:'Zha',3:'Gen',4:'FF',5:'pp'}pp= Dic.setdefault (6,'PPP') print (DIC) print (PP) result: {1:'Kong',2:'Zha',3:'Gen',4:'FF',5:'pp',6:'PPP'}PPP

9, Update: With Dic2 updated Dic1: If the Dic2 key does not exist in Dic1, then Dic2 inserted into Dic1, or more Dic2 key value, update Dic1

Dic1 = {'Name':'Kong',' Age': -}dic2= {'Name':'hucli','Sex':'M'}print (Dic1,dic2) dic1.update (dic2) print (DIC1) results: {' Age': -,'Name':'Kong'} {'Name':'hucli','Sex':'M'}{' Age': -,'Name':'hucli','Sex':'M'}

10. Values: Returns all values of the dictionary

Object Providing a view on D's valuesdic1 = {'Name':' Kong ','age':}print (dic1.values ()) Result: Dict_values ([  - ' Kong '])

Dict of Python data types

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.