Briefly summarize four data structures commonly used in python
table of Contents
First, a list of
-
Create a list
-
Additive element (append, extend, INSERT)
append the same point that both extend the list by adding the tail element, and extend in a plurality of elements, when added, will not split; is directly and integrally append way to add
addition, also by way of insert, the additive element to achieve the specified position
-
Remove elements (remove)
-
Clear List (clear)
-
Find an element (index)
index lookup way: index (looking_for_value, start, end ), as long as the elements found satisfying the condition, i.e., returns the corresponding index; if not find, a ValueError error occurs
generally end may be omitted, to the end of the list by defaultSmall scale chopper: the existing 10 students, which is arbitrarily assigned to three classrooms in
Second, the tuple
- Since no changes of tuples, so there is no tuple element adding and removing operation, conventional methods tuple index, count
Note: When an internal list containing the tuple, can be modified list elements contained in the
Third, Dictionary
python mapping of one type, such as a key-value pair in the form of key-value data storage
-
create
-
Add
Method a: directly specify a key value which corresponds
Method two: setDefault method, when the key-value pair is added, when the value is not specified, the default is None
-
Delete
pop method of the list remove similar, but popitem list of pop is similar -
Find
dictionary [ 'key'] and dictionary.get ( 'key') can achieve the look, when the key is not present, the former will lead to error, which will return None -
Traversal
Fourth, the collection
Is an unordered set of hash values may be arranged, as a key dictionary.
- Creating
Note: When you create a non-empty set, the difference between the dictionary - Add to
- delete
- Intersection, and complement, the difference
of which (blue) as shown in FIG complement crossing: