Here are some ways to summarize the python3.4 version list today.
List:
1, the list is like a linear container, but much more than the C + + lis t extension, the list of elements can be the same type, or can contain various types, such as the list of nested another table
2, the list index is also starting from 0, but can also be accessed from after, L1[-1] represents the last element in L1 can slice a list
3, the list can do addition, do multiplication, the string can also be seen as a list of characters
4, in statement, determine whether an object is in a string/list/tuple
5. The NOT statement indicates the negation of the following
6. Len can detect the number of elements in a string/list/meta-ancestor/dictionary
7, Max can return the largest element, Min returns the smallest element
8, Len (list) returns the length of the list
Del List[i] Delete the i+1 variable specified in list
9. Slice: Slice refers to a part of the extraction sequence, in the form of: List[start:end:step]. The rule is that the default step size is 1, but it can also be customized. The slice operation is similar to a call to a function that returns a new list of values,
Slice l1[x:y: z] is a semi-open interval (z is usually not written), such as L1[1:3] returns a list from the beginning of the l1[1] to the end of L1[2],x does not write to start from the beginning, and y does not write the expression until the end of the list, l1[3 >
Z is used to represent the step, the default is 1, can be considered in this interval for each z element to take one (take the first), can be negative, indicating from back to front traversal
List of methods:
1, append (x) append to the end of the chain
2. Clear () clears the list element
3. Shallow copy of copy () List
4, Extend (L) append a list, equivalent to + =
5, insert (i,x) in position i insert X, the rest of the elements push backwards, if I is greater than the list length, at the end of the add, if I is less than 0, at the beginning to add
6. Remove (x) removes the first element with the value x, and throws an exception if it does not exist
7. Reverse () Reverse sequence
8, Pop ([i]) Delete the element of position I and return it, the default can not write I, delete the last element, there is no error. Pop does two things: delete the last element of the list, and then return the value of the deleted element.
9, index (x) returns the position of the first occurrence of x in the list, and throws an exception if it does not exist
10, COUNT (x) returns the number of occurrences of x
11. Sort (Key=none, reverse=false) sorts the original list, returns None, has two optional parameters, key and reverse, default to ascending order
>>> 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 = [up]
>>> L2 = [3,4]
>>> L1 + L2
[1, 2, 3, 4]
>>> 5 * L1
[1, 2, 1, 2, 1, 2, 1, 2, 1, 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
>>> #赋值
>>> L1[1] = 5
>>> L1
[1, 5, 3, 4, 2]
>>> #删除
>>> del l1[1]
>>> L1
[1, 3, 4, 2]
>>> #分片赋值
>>> 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 ', ' ABCD ', ' BC ', ' CD ', ' abc ']
>>> l1.sort (key = len)
>>> L1
[' A ', ' BC ', ' CD ', ' CCC ', ' abc ', ' ABCD ']
Multiple ways to remove a list from Python
Today encountered a problem, at the casual tips of colleagues, the use of itertools.groupby this function. But this thing is still useless at last.
The problem is to go to the ID of a list, go to the weight and then make sure the order is not changed.
>>>ids = [ 1,2,3,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 works, but it doesn't look cool enough.
2. Use Set
Another solution is to use set and sort:
>>>ids = [1,4,3,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]
>>>ids = [ 1,4,3,3,4,2,3,4,5,6,1]
>>> Ids.sort ()
>>>it = Itertools.groupby (IDs)
>>> For K, G in it:
print k< /span>
about the Itertools.groupby principle can be seen here:/http Docs.python.org/2/library/itertools.html#itertools.groupby
4. Use reduce
>>>ids = [1,4,3,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]
Where the lambda x,y:x if y in x else x + [y] is equivalent to a lambda x,y:y in X and X or X+[y]
The idea is to turn IDs into [[], 1,4,3,......] and then take advantage of the features of reduce.
Python Data type-----list