"Python Dict" python dictionary operation

Source: Internet
Author: User

Python dictionary

is an unordered, data type stored in key-value pairs , with strong data affinity and a unique mapping data type. key: Must be a hash (immutable data type: string, number, tuple, bool) value and is unique

None: None is an empty object, ' is a string object, none is a special data type

Dictionary method:

dic={"Name" = ' Alex ', ' sex ' = ' men '} up = {' name ': ' Ritian ', ' sex ': ' Men ', ' job ': ' It '}

Increase:

(1) The first method: Direct dic["name"] = 20, this method, if the key already exists, will overwrite the previous value

(2) The second method: SetDefault (k,v), the dictionary calls the function, the parameter "K" is required, "V" if the default is None, this function can avoid overwriting the existing key.

By deleting:

(1) The first kind: Dic.pop ("Sex", None), the pop function parameter is required, the ' sex ' parameter is a dictionary key, if the key does not exist will be an error. The second parameter is optional, if you do not fill in the second parameter, the deleted key does not exist, and if you add "None" argument, Deleted key not found, no error, return none

If the dictionary is empty and the function is used, the dictionary is empty times wrong

(2) The second type: Dic.popitem () The function does not have to fill in the parameters, it randomly deletes a key-value pair. If the dictionary is empty, use this function to error

(3) The third type: Dic.clear (), the function does not have to fill in the parameters, empty the dictionary.

(4) Fourth type: Del dic[' name '] if the deleted key does not exist error

Change:

(1) The first type: dic[' name '] = ' Ritian '
(2) Second type: Dic.update (UP), update function, up dictionary add and overwrite dic

DIC = {"name":'Alex','Job':'changping'}up= {"name":'Ritian','Job':'changping','Sex':' Men'}Print(Dic.update (UP)) None the following is the correct method output: dic.update (UP)Print(DIC) {'name':'Ritian','Job':'changping','Sex':' Men'}

Check:

(1) The first type: Dic.get (' name ', none), the function, if the value corresponding to the key is not found, return none, the second parameter can be specified by default, return None

(2) Second type: print (dic[' name ')

(3) The Third kind: the following

Up = {"name":'Ritian','Job':'changping','Sex':' Men'} forIinchUp#the first key value to remove the dictionary    Print(i) forKeyinchUp.keys ():#the second key value of the dictionary is removed    Print(Key) forValueinchUp.values ():#Remove Value    Print(value)

(4) The fourth type:

    

 for key, value   in Up.items ():#up.items () returns a traversed (key, value) tuple array    print (Key,value)  #key/value Assign output separately

(5) The fifth type:

' Ritian ' in the UP

"Python Dict" python dictionary operation

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.