[Python] Use dict and set, and python use dictset

Source: Internet
Author: User

[Python] Use dict and set, and python use dictset

Dict stands for dictionary. Similar to map, dict uses key-value pairs for storage, which is fast.

Use {"key": value} to initialize the dict object, for example, d = {"name": "taoshihan "}

Store data by key, for example, d ["age"] = 100

Use the in keyword to determine whether the key exists. For example, res = "age" in d, res is true.

Use the get () method of the dict object to obtain the data. Parameter: key

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

 

Features:

Queries and inserts are fast and will not slow down as keys increase.

Large memory usage

The key must be an unchangeable object.

 

The algorithm used to calculate the position of value based on the key is called the Hash algorithm)

List is a variable object and cannot be used as a key. str is an immutable object and can be used as a key.

 

Set

Create a set and use the set () method. Parameter: list object. duplicate elements are automatically filtered out in the set.

Example: mySet = set ([1, 2, 3])

Use the add (key) and remove (key) Methods of the set object to add and delete elements.

 

The only difference between set and dict is that the corresponding value is not stored.

 

Tuple

Ordered list tuples cannot be modified once initialized. If its elements are a list, the element list can be changed.

Use brackets () to initialize tuple. parameters: Elements and elements

Example: myTuple = ("zhangsan", "lisi ")

Define tuple with only one element. Use (), parameter: element to avoid ambiguity and add a comma.

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.