[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.