[Python] using dict and set

Source: Internet
Author: User

Dict Full name is dictionary, similar to map, use key value to store, fast

Initialize the Dict object with {"Key": value}, for example: d={"name": "Taoshihan"}

Data is stored via key, for example: d["Age"]=100

Use the IN keyword to determine if key exists, for example: res= "Age" in D,res is true

Use the Get () method of the Dict object to get the data, parameters: Key

For example: Age=d.get ("Age"), the age is 100

Characteristics:

Find and insert very fast and do not slow as key increases

Need to consume a lot of memory

Key must be an immutable object

The algorithm for calculating the position of value according to key is called the hash Algorithm (hash)

List is a mutable object cannot be an immutable object as KEY,STR can be a key

Set

Create a set, using the Set () method, Parameters: List object, repeating element is automatically filtered in set

Example: Myset=set ([+])

Add and remove elements using the Add (key) and remove (key) methods of the Set object

The only difference between set and dict is that it does not store the corresponding value

Tuple

There is a sequence table tuple, once initialized cannot be modified, if its element is a list, then the element list can be changed

Use () parentheses to initialize a tuple, parameter: element, Element

Example: mytuple= ("Zhangsan", "Lisi")

Define tuple with only one element, use (), parameter: element, avoid ambiguity add a comma

[Python] using dict and set

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.