Slice
Forward index (left-to-right):
>>> a = [1,2,3,4,5,6,7]
>>> A[0:4:1] # 1 is the step
[1, 2, 3, 4]
Reverse index:
>>> A[-1:-4:-1] (right-left
)
[7, 6, 5]
>>>
Default index:
>>> a[1:]
[2, 3, 4, 5, 6, 7]
Adding actions to a list
>>> a = [1,2,3,4]
>>> B = [5,6,7,8]
>>> A + b
[1, 2, 3, 4, 5, 6,7,8] # Generate a new list
>>>
Extend: Takes a parameter and adds each element of the parameter to the original list, modifying the list in place instead of creating a new list
>>> a = [+ +]
>>> B = [4,5,6]
>>>
>>> A.extend (b)
>>> A
[1, 2, 3, 4, 5, 6]
>>>
Append: Add any object to the end of the list
>>> a = [+ +]
>>> A.append (4)
>>> A
[1, 2, 3, 4]
>>> a.append ([3,4,5])//Insert List
>>> A
[1, 2, 3, 4, [3, 4, 5]]
Insert: Inserts any object into the list to control the insertion position.
[1, 2, 3, 4, [3, 4, 5]]
>>>
>>> A.insert (1, "abc")
>>> A
[1, ' abc ', 2, 3, 4, [3, 4, 5]
Modify actions for a list
Modifying the list itself requires only the direct assignment of the action.
>>> a = [+ +]
>>> a[1]= ' Test '
>>> A
[1, ' Test ', 3]
Delete operation
Del: We delete the element at the specified location by index
>>> a = [1,2,3,4,4]
>>> del A[0]
>>> A
[2, 3, 4, 4]
Remove: Removes the first matching value from the specified value in the list. If it is not found, an exception will be thrown.
>>> A
[2, 3, 4, 4]
>>> A.remove (4)
>>> A
[2, 3, 4
Pop: Returns the last element and removes it from the list.
>>> a = [1,2,3,4,5,6,7]
>>> A.pop ()
7
>>> A
[1, 2, 3, 4, 5, 6]
List member relationships
In no in we can determine whether an element is in the list, return a bool type, if True is in the lists, and vice versa.
>>> a = [+ +]
>>>
>>> 2 in a
True
>>> 5 in a
False
>>> 5 Not in a
True
>>>
-
List derivation
1. First iterate all the contents of iterable, each iteration, put the corresponding content in the iterable into the Iter_var, and then apply the content of the Iter_var in the expression, Finally, a list is generated with the calculated value of the expression.
than we're going to generate a list containing 1 to 10
>>> [x for x in range (1,11)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
>>
2. Added the judgment statement, only satisfies the condition content to put in the iterable the corresponding content in the Iter_var, then applies the content of the Iter_var in the expression, and finally generates a list with the calculated value of the expression.
to generate all the odd list of numbers from 1 to 10.
Range (1,11,2)
>>> Range (1,11,2)
[1, 3, 5, 7, 9]
>>>
Sort Flip: Sort,reverse
Sort: Modifies the original list directly, and its return value is None
>>> a = [33,11,22,44]
>>> B = A.sort ()//none
>>>
>>> b
>>> if B is None:
... print ' haha '
>>> if B is None:
... print ' haha '
...
haha
Reverse function: Reverses a list, and its return value is None
>>> A
[11, 22, 33, 44]
>>> B = A.reverse ()
>>> b
>>> A//Direct view a list variable can see the effect of flipping
[44, 33, 22, 11]
>>>
This article is from "Never give up!" "Blog, be sure to keep this provenance http://rockyjun.blog.51cto.com/8504719/1544886