Lists and fields, both of which are almost the main working components of all Python scripts. They can be modified in place, can be increased or shortened as required, and contain objects of any kind or are nested.
First, List
Main properties of the list:
* An ordered set of arbitrary objects
Functionally, a list is where other objects are collected, which can be thought of as groups. Each item contained in the list maintains a left-to-right position order (they are sequences)
* Read by offset
Like a string, it can be indexed by the offset of a list object to read a portion of the object. Tasks such as self-sharding and merging can be done.
* variable length, heterogeneous and arbitrary nesting
Lists can grow or shorten on the ground, and can contain objects of any type. Supports arbitrary nesting and can create sub-lists of lists of sub-lists.
* Categories that belong to variable sequences
The list can be modified in place. Sequence operations work the same way in a list as in a string. The only difference is that when operations such as merging and sharding are applied to the list,
Returns a new list instead of a new string. However, lists are mutable because they support other operations that strings do not support, such as delete and index assignment operations.
They are all in place to modify the list.
* Object reference Array
The list contains references to 0 or more other objects. Contains any object, which can be a dictionary, meaning that a dictionary can be nested. Inside the Python interpreter, the list is a C array instead of a link structure. Common, Representative list operations. More information can be found in Python's standard library or help (list) or Dir (list) to view a complete list of lists methods.
Operation explanation
l1=[] an empty list
l2=[0,1,2,3] Four items: index 0 to 3
l3=[' abc ', [' def ', ' Ghi '] nested sub-list
L2[i] Index
Index of L2[I][J] Index
L2[I:J] Shards
Len (L2) seeking length
L1+l2 Merging
l2* Repeat
For x in L2 iteration
3 in L2 members
L2.append (4) Method: Increase the number of individual objects
L2.extend ([5,6,7]) method: Increase the number of objects
L2.sort () Method: Sort
L3.index (' abc ') method: Find Object index by object (action opposite index)
L2.insert (i,x) method: Insert (insert X at position I).
Insert () Inserts a position that exceeds the range of the list and inserts the last position of the list.
L2.reverse () Method: Flip
L2.remove (' B ') method: Remove the parameter of the method fill in the object
L2.pop (1) Method: The parameter of the Remove method fills in the index
Del L2[k] Cropping: K is the cropped index
Del L2[i:j] Cropping:
L2[i]=1 Index Assignment
l2[i:j]=[4,5,6] Shard Assignment
Rang (4) generating an integer list/tuple
Xrange (1,4)
L4=[x**2 for x in range (5)] List parsing
Second, the actual application of the list
1. Basic list operation
Merge and Repeat, and the string is basically the same, but the result is a new list, not a string.
>>> Len ([+])
3
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> [' Diege ', 18]*3
[' Diege ', ' diege ', ' Diege ', 18]
>>> 3 in [+]
True
>>> for x in [rint]:p x,
...
1 2 3
Note that the "+" action is the same as in the string, but it is worth noting that "+" must be the same type of sequence on both sides. You cannot merge a list with a string
Together, unless you first convert the list to a string.
>>> STR ([up]) + ' 34 '
' [1, 2]34 '
>>> [1,2]+list (' 34 ')
[1, 2, ' 3 ', ' 4 ']
2, index, Shard, matrix
The result of indexing a list is the object at the offset you specify (regardless of type), and a new list is often returned when the list is fragmented
>>> l=[' Diege ', ' Lily ', ' keylly '
>>> L[1]
' Lily '
>>> L[-1]
' Keylly '
>>> l[1:]
[' Lily ', ' keylly ']
>>> ll=[[1,2,3],[4,5,6],[7,8,9]]
>>> Ll[2]
[7, 8, 9]
>>> Ll[1][2]
6
Third, the original change list
The list variable allows you to change the operation of the list object on the field, and the following actions can directly modify the list object without forcing you to create a new copy like a string. Because Python only handles object references, it is necessary to distinguish between modifying an object and creating a new object.
1. Index and Shard Assignment
Assigns a specific item (offset) or an entire fragment (shard) to change the contents of the list.
>>> l=[' Diege ', ' Lily ', ' keylly '
>>> l[1]= ' Tom '
>>> L
[' Diege ', ' Tom ', ' keylly ']
>>> l[0:2]=[' Boo ', ' July ']
>>> L
[' Boo ', ' July ', ' keylly ']
>>> L
[' Lily ', ' Kelly ', ' X ', [1, 2, 3]
>>> l[0:2]=[' Lily ', ' Kelly ', ' Fang '
>>> L
[' Lily ', ' Kelly ', ' Fang ', ' X ', [1, 2, 3]
The Shard assignment can be understood in two steps. First delete, then insert.
List nested dictionaries
>>> L
[' Diege ', {' tow ': 789, ' one ': 456}, 999]
>>> L[1]
{' Tow ': 789, ' one ': 456}
>>> l[1][' one ']
55W
2. List method calls
1), append () append an element
Note: Append is an Append object, a string, a list, etc.
>>> L
[' Boo ', ' July ', ' keylly ']
>>> l.append (' June ')
>>> L
[' Boo ', ' July ', ' keylly ', ' June ']
>>> L
[' Lily ', ' Kelly ', ' Fang ', ' X ', [1, 2, 3], ' Wang '
>>> l.append ([' Wang ', ' Fei '])
>>> L
[' Lily ', ' Kelly ', ' Fang ', ' X ', [1, 2, 3], ' Wang ', [' Wang ', ' Fei ']
2), extend () insert multiple elements at the end
Placing a string breaks into character insertion, so the inserted parameter is placed in the list.
>>> l.extend (' test ')
>>> L
[' Lily ', ' Kelly ', ' Fang ', ' X ', [1, 2, 3], ' Wang ', ' t ', ' e ', ' s ', ' t ']
>>> del l[5:]
>>> L
[' Lily ', ' Kelly ', ' Fang ', ' X ', [1, 2, 3]
>>> l.extend ([' Zhang ', ' Wang '])
>>> L
[' Lily ', ' Kelly ', ' Fang ', ' X ', [1, 2, 3], ' Zhang ', ' Wang '
>>> l=[1,2]
>>> l.extend ([3,4,5])
>>> L
[1, 2, 3, 4, 5]
3), insert () inserts the location and data to specify the insertion
Insert () Inserts a position that exceeds the range of the list and inserts the last position of the list.
>>> L.insert (1,5)
>>> L
[1, 5, 4]
>>> L.insert (2,[6,7,9])
>>> L
[1, 5, [6, 7, 9], 4]
>>> l=[' Diege ', ' Lily '
>>> L.insert (1, ' Kelly ')
>>> L
[' Diege ', ' Kelly ', ' Lily ']
4), pop () Delete an element
Pop () method: The parameter of the Remove method fills in the index, the last one by default
>>> L
[1, 2, 3, 4, 5]
>>> L.pop ()
5
>>> L
[1, 2, 3, 4]
You can specify the index to delete
>>> L.pop (2)
3
>>> L
[1, 2, 4]
5), remove () Remove must specify object name
>>> L.remove (2)
>>> L
[1, 4]
6), sort () sorting
>>> L.sort ()
>>> L
[' Boo ', ' July ', ' June ', ' keylly ']
7), reverse () sequence reversal
>>> L.reverse ()
>>> L
[' keylly ', ' June ', ' July ', ' Boo ']
>>> L
[' Diege ', ' Kelly ']
>>> l.remove (' Diege ')
>>> L
[' Kelly ']
Note the find () and replace () methods are not supported, and the two methods belong to the string
3. Other common list operations
Del L[1]
>>> L
[1, 5, [6, 7, 9], 4]
>>> del L[1]
>>> L
[1, [6, 7, 9], 4]
>>> del l[1:]
>>> L
[1]
>>> l=[' Diege ', 1,5]
>>> l[1:]=[]
>>> L
[' Diege ']
5 three ways to create 0 lists
[0]*5
>>>
>>> [0 for I in range (5)]
>>> l=[]
>>> for I in range (5):
... L.append (0
Python Learning Notes collation (v) list in Python.