Python data type ----- List, python data type -----
Today, we will summarize some operations on the python3.4 version list.
List ):
1. The list is like a linear container, but it is much larger than the lis t extension of C ++. The elements in the list can be of the same type or contain various types, for example, nesting another list in the list
2. list indexes start from 0, but can also be accessed from the back. L1 [-1] indicates that the last element in L1 can be sliced into the list.
3. The list can be added and multiplied, and the string can also be seen as a list of characters.
4. in statement to determine whether an object is in a string/LIST/tuples
5. The not statement indicates a negative response
6. len can detect the number of elements in the string/LIST/ancestor/dictionary.
7. max returns the maximum element, while min returns the minimum element.
8. len (List) returns the length of the List.
Del list [I] Delete the I + 1 variable specified in the list
9. Slice: A Part Of The extraction sequence, in the form of list [start: end: step]. The extraction rule is: Generally, the default step is 1, but can also be customized. The Slice operation is similar to calling a function and returns a new list,
Slice L1 [x: y: z] is a semi-open closed interval (z is usually not required ), for example, L1 [] returns a list from L1 [1] to L1 [2], excluding L1 [3],If 'X' is not written, it indicates that it starts from the beginning, and if 'y' is not written, it indicates that it ends until the end of the list,
Z is used to represent the step size. The default value is 1. It can be regarded as taking one (take the first) for every z element in this range. It can be a negative number, indicating traversing from the back to the front
List Method:
1. append (x) to the end of the chain
2. clear () clears the list elements.
3. Shallow copy of the copy () List
4. append an extend (L) List, equivalent to + =
5. insert (I, x) insert x at position I, and push other elements backward. If I is greater than the list length, add it at the end. If I is less than 0, add it at the beginning.
6. remove (x) deletes the first element with the value of x. If it does not exist, an exception is thrown.
7. reverse () sequence
8. pop ([I]) deletes the element of position I and returns it. By default, I is not allowed. If the last element is deleted, an error occurs. Pop deletes the last element of the list and returns the value of the deleted element.
9. index (x) returns the position where x appears for the first time in the list. If no position exists, an exception is thrown.
10. count (x) returns the number of times x appears.
11. sort (key = None, reverse = False) sorts the original list and returns None. There are two optional parameters: key and reverse. The default value is ascending.
>>> L1 = [1, 2, 3, 4, 5, 6]
>>> L1 [1: 3]
[2, 3]
>>> L1 [: 3]
[1, 2, 3]
>>> L1 [1:]
[2, 3, 4, 5, 6]
>>> L1 [-3:-1]
[4, 5]
>>> L2 = L1 [:]
>>> L2
[1, 2, 3, 4, 5, 6]
>>> L1 [: 2]
[1, 3, 5]
>>> L1 [:-1]
[6, 5, 4, 3, 2, 1]
>>> L1 = [1, 2]
>>> L2 = [3, 4]
>>> L1 + L2
[1, 2, 3, 4]
>>> 5 * L1
[1, 2, 1, 2, 1, 2, 1, 2, 2]
>>> L1
[1, 2, 3, 4, 2]
>>> 3 in L1
True
>>> 5 in L1
False
>>> 3 not in L1
False
>>> 5 not in L1
True
>>> Len (L1)
5
>>> Max (L1)
4
>>> Min (L1)
1
>>># Assign values
>>> L1 [1] = 5
>>> L1
[1, 5, 3, 4, 2]
>>># Delete
>>> Del L1 [1]
>>> L1
[1, 3, 4, 2]
>>># Multipart assignment
>>> L1 [2:] = [6, 7, 8]
>>> L1
[1, 3, 6, 7, 8]
>>> L1 [1: 3] = []
>>> L1
[1, 7, 8]
>>> L1
[8, 7, 2, 1]
>>> L1.sort ()
>>> L1
[1, 2, 7, 8]
>>> L1.sort (reverse = True)
>>> L1
[8, 7, 2, 1]
>>> L1 = ['A', 'ccc ', 'abc', 'bc', 'cd', 'abc']
>>> L1.sort (key = len)
>>> L1
['A', 'bc', 'cd', 'ccc ', 'abc', 'abc']
Multiple methods for removing list duplicates in python
I encountered a problem today. I used the itertools. groupby function at my colleague's prompt. However, this is useless.
The problem is that the IDs in a list are de-duplicated, and the order must remain unchanged after de-duplicated.
1. intuitive method
The simplest idea is:
>>> Ids = [1, 2, 3, 4, 2, 3, 4, 5, 6, 1]
>>> News_ids = []
>>> For id in ids:
If id not in news_ids:
News_ids.append (id)
>>> Print news_ids
This is also feasible, but it does not look good enough.
2. Use set
Another solution is to use set and sort:
>>> Ids = [1, 4, 3, 4, 2, 3, 4, 5, 6, 1]
>>> News_ids = list (set (ids ))
>>> News_ids.sort (key = ids. index)
>>> New_ids
[1, 4, 3, 2, 5, 6]
3. Use itertools. grouby
The article mentioned itertools. grouby at the beginning. This can be used without considering the list order:
>>> Ids = [1, 4, 3, 4, 2, 3, 4, 5, 6, 1]
>>> Ids. sort ()
>>> It = itertools. groupby (ids)
>>> For k, g in it:
Print k
For the itertools. groupby principle can be viewed here: http://docs.python.org/2/library/itertools.html#itertools.groupby
4. reduce
>>> Ids = [1, 4, 3, 4, 2, 3, 4, 5, 6, 1]
>>> Func = lambda x, y: x if y in x else x + [y]
>>> Reduce (func, [[],] + ids)
[1, 4, 3, 2, 5, 6]
Lambda x, y: x if y in x else x + [y] is equivalent to lambda x, y: y in x and x or x + [y].
The idea is to first change ids to [[], 3,...], and then use the reduce feature.