Python Learning note 06-Dictionaries

Source: Internet
Author: User

Dictionary:

Unique mapping type in Pytho, unordered list (hash table)

Objects are mutable, but dictionary keys must use immutable objects, and different types of key values can be used in a dictionary

Definition method:-{} such as: Dic={name: ' Zzc ', Age:19,gender: ' Man '}

-Using the Factory Method Dict () column fdict=dice ([' X ', 1],[' Y ', 2])

-Built-in method: Fromkeys () The elements in the dictionary have the same value, default to None

Column: Ddict=[].fromkeys (' x ', ' Y '),-1)
Operation:

-Added: dic[' key ']= ' value '

-Modified: dic[' key ']= ' value '

-Delete a key and Value:del (dic[' key ') in the dictionary

-Delete and display the deleted value after deletion: dic.pop[' key '

-Delete all elements of the Dictionary: Dic.clear ()

-Delete entire dictionary: del dic

Method of the Dictionary:

+dic.clear: Delete all elements in the dictionary

+dic.fromkeys (Seq,val=none) creates and returns a dictionary with the elements in the SEQ, and Val is the default value set

+dic.get (Key,default=none): Returns the value of key if the key does not exist return Fefault specified

+dic.has_key (Key): Determine if there is a key in the dictionary, it is recommended to use in and not in instead of

+dic.keys () returns a list of keys in the dictionary

+dic.items () Returns a list of the corresponding tuples of the key value

+dic.iter* () Iteritems () Iterkeys () returns the iteration value instead of the list

+dic.setdefault (Key,default=none) with set, if key exists return its value, if key does not exist Dic[key]=default

+dic.update (DIC1) adds the key-value pairs in the Dic1 to the dictionary dic, and if duplicates overwrite, entries that do not exist in the original dictionary are added

+dic.valuse () Returns a list of all values in the dictionary

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Python Learning note 06-Dictionaries

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.