Python---Dictionary-related operations

Source: Internet
Author: User

Dictionary features: Dictionary keys are unique, the dictionary is unordered can not be the subscript to take the value, through key to take the following examples of the dictionary related usage names={' zhu ': [, ' NV ', ' Shijiazhuang '], ' Yue ': {' age ': '}}--- The value of the dictionary can be a list, multiple kv separated by commas one, values: Names.get (' Zhu ')---does not exist return nonenames[' Zhu ')---does not exist will be error two, add names[' yue1 ']=[18,1993] three, Change names[' Zhu ']=[19] IV: Delete 1, del names[' Zhu '] 2, Names.pop (' Zhu ')---Delete the specified Key3, Names.popitem ()---Randomly delete a value five, built-in method: names={" Zhu ":" NV "}ages={" Age ": 18}names.keys ()---Print all key values Names.values ()---Print all value values Names.setdefault (' Juan ', [ 18,19931116])---Set key existence does not modify the original value, does not exist the new, the parameter is (key,vaule)--vaule optional, not written as Nonenames.update (ages)--- Add the dictionary ages to the dictionary names---Extended DictionaryNames.has_key (' Zhu ')----Determine if there is this keyThis method exists in Python2' Zhu ' in names---determine if key is Names.items () in the dictionary---convert the dictionary to a list to get the values of key and values in the dictionary, the dictionary will be converted to a list for k,v in Names.items ()---deprecated Print (k,v) get keyfor K in Names () print (k) Get key and values, still the way of the dictionary-more efficient for K in Names:print (K,names[k])

Python---Dictionary-related operations

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.