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 '])