Python Basics-Dict and set

Source: Internet
Author: User

Python built-in dictionary: dict support, Dict full name dictionary, also known as map in other languages, using key-value (Key-value) storage, with extremely fast search speed

Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.

Dictionary = {' name ': ' Bob ', ' age ': ' City ': ' Hangzhou '}dictionary[' city '] ' Hangzhou '

In addition to the values that are placed during initialization, you can also put

>>> dictionary = {"name": "Bob", "age": A, "City": "Hangzhou"}>>> print dictionary{' city ': ' Hangzhou ', ' Age ': ' The ' name ': ' Bob '}>>> dictionary[' score ']= 90>>> print dictionary{' city ': ' Hangzhou ', ' age ': 12 , ' score ': +, ' name ': ' Bob '}

Since a key can only correspond to one value, the value is placed multiple times for a key, and the following values will flush the previous value out:

>>> dictionary["Score"]= 100>>> dictionary["age"]= 30>>> print dictionary{' city ': ' Hangzhou ', ' age ': +, ' score ': +, ' name ': ' Bob '}

If key does not exist in Dict, it will be an error

>>> dictionary["Age"]30>>> dictionary["ages"]traceback (most recent call last):  File "<stdin > ", Line 1, in <module>keyerror: ' Ages '

To avoid a key that does not exist, there are two ways to avoid errors, one is to in determine if a key exists:

>>> ' age ' in dictionarytrue>>> ' ages ' in Dictionaryfalse

The second is the Get method provided by Dict, if key does not exist, you can return none, or the value you specified:

>>> dictionary.get ("Age")       #get () method to get key is the age of Value30                           >>> dictionary.get ("Ages", "ages not Exist ")  #key不存在, return the value you have set for ' ages not exist ' >>> dictionary.get (" ages ")       

Elements that are duplicated in set are automatically filtered out

>>> s = set ([1,2,2,3,3,4,5]) >>> sset ([1, 2, 3, 4, 5])

You can add a delete element to a set by using Add (key) and remove (key)

>>> S.add ("Test") >>> sset ([1, 2, 3, 4, 5, ' Test ']) >>> s.remove (4) >>> sset ([1, 2, 3, 5, ' Test '])

Python Basics-Dict and set

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.