List
The list is the most flexible, ordered collection object type in Python. Unlike strings, a list can contain objects of any type: numbers, strings, or even other lists. A list is a mutable object that supports actions that are modified in situ.
The list of Python is:
- An ordered set of arbitrary objects
- Read by offset
- Variable-length, heterogeneous, and arbitrary nesting
- Categories that belong to a mutable sequence
- Object reference Array (a reference to the object is stored in the list, not a copy of the object)
List in real-world apps
>> Basic List Operations
Because the list is a sequence, it supports many of the same operations as strings. The response of the list to the "+" and "*" actions is similar to the string, and the two operations mean merging and repeating, but a new list, not a string.
>> indexes, shards, and matrices
For lists, index and shard operations are essentially the same as those in strings. However, the result of indexing the list is the object at the offset you specify (regardless of the type), and a new list is often returned when the list is fragmented.
Matrix = [[1,2,3],[4,5,6],[7,8,9]]
The code above defines a 3*3 two-dimensional matrix.
>> Modify List
Index vs. Shard Assignment
When using a list, you can assign it to a specific item (offset) or an entire fragment (shard) to change its contents, and the index and shard assignments are modified in situ, and they modify the list directly instead of generating a new list as a result. Index assignments in Python are very similar to C and most other languages--python the object reference with a new value instead of the specified offset
Invocation of a list method
The most commonly used list method is append, which can simply add a single item (object reference) to the end of the list. Unlike merging, append allows you to pass in a single object instead of a list. L.append (X) is similar to the result of l+[x], but the former will modify the L in situ, and the latter will generate a new list. Another common method is sort, which sorts the list in place. Sort is the default value (in this case, a string comparison) using the Python standard comparison test, and is sorted in ascending order. In addition, we can modify the sorting behavior by passing in a keyword parameter-this is a special "name=value" language in a function call that is passed by name.
It is important to note that append and sort are modified in-place for the list object, and the return result of the method does not return a list (technically, the return value of both is none). If you write a statement like L=l.append (X), you will not get the modified value (in fact, you will lose the entire list of references).
- Reverse: Reverse list in situ
- Extend: Inserting multiple elements at the end
- Pop: Delete the last element while returning the deleted value
- Remove: Deletes an element by value
- Insert: Inserts an element at offset
- Index: Find an offset for an element
Dictionary
If you think of a list as an ordered collection of objects, you can think of the dictionary as an unordered collection, the main difference being that the elements in the dictionary are accessed by keys rather than by offsets. The main properties of the Python dictionary are as follows:
- Read by key instead of offset
- Unordered collection of arbitrary objects
- Variable length, heterogeneous, arbitrary nesting
- belongs to a mutable mapping type
- Object reference table (hash list) (the dictionary stores a reference to an object, not a copy of the object)
- Dictionaries in real-world applications
>> Modify Dictionary in situ
As with lists, assigning values to indexed values that already exist in the dictionary alters the values associated with the index. However, unlike lists, whenever a new dictionary key is assigned (a key that was not previously assigned), a new element is generated within the dictionary.
>> methods of other dictionaries
- Keys: Returns the list of keys for the dictionary
- Values: Returns a list of value in a dictionary
- Items: Returning a dictionary (key,value) to a tuple
- Update: Merge
- Pop: Removes a key from the dictionary and returns its value
>> Dictionary usage considerations
- Invalid sequence operation
- Assigning a value to a new index adds an item
- The key is not always a string
>> other ways to create a dictionary
#Method1D = {' name ': ' Mel ', ' Age ': $ #Method2D = {}d[' name ']= ' Mel ' d[' age ']=45#method3d = Dict (name= ' Mel ', age=45) # METHOD4D = Dict ([' Name ', ' Mel '), (' age ', 45)])
The same dictionary is created in all four forms.
Dictionary changes in the >>python3.0
The function of the dictionary has changed in Python3.0, specifically, the dictionary in Python3.0:
- Supports a new dictionary parsing expression, which is the "next of kin" for list and set parsing
- For D.key,d.values and D.items methods, returns an iterative view instead of a list
- Because of the previous point, a new encoding is required to traverse through the sort key
- No longer directly supports relative size comparisons-instead of manually comparing
- There is no longer a D.has_key method-instead, use the in member relationship test
Dictionary View
In Python3.0, the keys, values, and items of the dictionary return the View objects, and in Python2.6, they return the actual list of results. The dictionary views in Python3.0 cannot be changed after they are created, and they can be dynamically reflected by making a point change to the dictionary after the View object is created:
D={' A ': 1, ' B ': 2, ' C ': 3}k = D.keys () V = D.values () del d[' B ']list (K)
In the preceding code, the result of the last line is [' A ', ' C '].
The Keys method returns objects that are similar to collections and support common operations such as intersection and set, and the values view is not the case because they are not unique, but the items result is yes if the (key,value) pair is unique and can be hashed.
Meta-group
Tuples are composed of simple objects. Tuples are very similar to lists, except that tuples cannot be modified in the same place (they are immutable) and are usually written in parentheses (rather than square brackets) in a series of items. Its properties are:
- An ordered set of arbitrary objects
- By offset access
- belongs to an immutable sequence type
- Fixed-length, heterogeneous, arbitrary nesting
- An array of object references
>> tuples in real-world applications
Special syntax for tuples: commas and parentheses
Because parentheses can also enclose expressions, if a single object in parentheses is a tuple object instead of a simple expression, Python needs to be specifically described. If you really want a tuple, just add a comma after this single element, before closing the parentheses.
x = (All) y = (40,)
In the preceding code, the first row of x is an integer, and the second row of Y is a tuple that contains an element 40.
Transformations, methods, and immutability
The operation of tuples is consistent with strings and lists, and the notable difference is that the "+", "*", and the new tuples are returned when the Shard operation is applied to the tuple, and the tuples do not provide the methods in the string, list, and dictionary.
>> Why do I have a list? tuples
The immutability of tuples provides some integrity, so you can ensure that tuples are not modified by another reference in the program, and the list is not guaranteed.