A detailed description of the use of dict and set in Python

Source: Internet
Author: User
The structure of a PHP-like array in Python has list,tuple,dict and set, where list, tuple, and set are similar to the PHP indexed array, and dict is similar to the associative array of PHP,
dict:

The structure of the dict represents a mapping relationship, similar to a PHP associative array, such as defining user information as follows:

Name:Yi_Zhi_Yusex:Mancountry:China

The use of list,tuple and set is not directly represented, because there is no structure to use the string to do key values, and dict can be, as follows

m = {"Name": "Yi_zhi_yu", "Sex": "Man", "Country": "China"}//Note here is ' {} ', not ' [] ' Print m[' name ']//yi_zhi_yuprint m["sex"]// Manprint m["Country"]//china

The Dict numeric object has a Get method that allows us to specify a default value when we get the specified key

    • Dict has the advantage of finding (hash key) and inserting (no order required) speed, but memory consumption is large
    • And the list is just the opposite.

Set:

Set is similar to List and Tuple, but the values in set are not duplicated
For example, to define a course of study, use Set definition:
Defined as follows:

Classes = Set ([u "Language", U "math", U "English"])//set keyword, in parentheses is a listprint classes//set ([u ' \u8bed\u6587 ', U ' \u6570\u5b66 ', U ' \u82f1\ u6587 '])

The added method is the add

Classes.add (U "Mathematics")

There will be no duplicate print classes//set ([u ' \u8bed\u6587 ', U ' \u6570\u5b66 ', U ' \u82f1\u6587 ']) classes.add (U "History") Print classes//set ([u ' \u8bed\u6587 ', U ' \u6570\u5b66 ', U ' \u5386\u53f2 ', U ' \u82f1\u6587 '])

The removal method is remove:

Classes.remove (U "History") print classes//Set ([u ' \u8bed\u6587 ', U ' \u6570\u5b66 ', U ' \u82f1\u6587 '])
  • 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.