1.pyhton Assertion
How to add an exception parameter for an ASSERT assertion statement
The assert exception parameter, in fact, is to add string information after the assertion expression to explain the assertion and better know where the problem is. The format is as follows:
assert expression [, arguments]
assert expression [, parameters]
Assert Len (lists) >=5, ' list element number is less than 5 '
Assert 2==1, ' 2 not equal to 1 ' 2.list complex sort
Data_list = []
data_list.append ({' Softname ': ' 1 ', ' Version ': ' 1.2.2.2 '})
data_list.append ({' Softname ': ' 7 ', ') Version ': ' 1.2.2.2 '}
data_list.append ({' Softname ': ' 5 ', ' Version ': ' 1.2.2.2 '})
data_list.append ({' Softname ': ' 2 ', ' Version ': ' 1.2.2.2 '})
data_list.append ({' Softname ': ' 3 ', ' Version ': ' 1.2.2.2 '})
Data_ List.append ({' Softname ': ' 9 ', ' Version ': ' 1.2.2.2 '})
#升序
data_list.sort (key=lambda obj:obj.get (' Softname ') , Reverse=false)
print data_list
#[{' softname ': ' 1 '}, {' Softname ': ' 2 '}, {' Softname ': ' 3 '}, {' Softname ': ' 5 '}, { ' Softname ': ' 7 '}, {' Softname ': ' 9 '}]
#降序
data_list.sort (key=lambda obj:obj.get (' Softname '), reverse=true)
print data_list
#[{' softname ': ' 9 '}, {' Softname ': ' 7 '}, {' Softname ': ' 5 '}, {' Softname ': ' 3 '}, {' Softname ': ' 2 '}, {' Softname ': ' 1 '}]
to sort the list, Python provides two methods
Method 1. Sorting using the List.sort function of list
List.sort (Func=none, Key=none, Reverse=false)
Python instance:
>>> list = [2,5,8,9,3]
>>> list
[2,5,8,9,3]
>>> list.sort ()
>>> List
[2, 3, 5, 8, 9]
Method 2. Sort by sequence type function sorted (list) (starting from 2.4)
Python instance:
>>> list = [2,5,8,9,3]
>>> list
[2,5,8,9,3]
>>> sorted (list)
[2, 3, 5, 8, 9]
two ways to distinguish:
Sorted (list) Returns an object that can be used as an expression. The original list does not change, creating a new sorted list object.
List.sort () does not return the object, changing the original list.
Other instances of sort:
Instance 1: Forward sort
>>>l = [2,3,1,4]
>>>l.sort ()
>>>l
>>>[1,2,3,4]
Example 2: Reverse sorting
>>>l = [2,3,1,4]
>>>l.sort (reverse=true)
>>>l
>>>[4,3,2,1]
Instance 3: Sort the second keyword
>>>l = [(' B ', 6], (' A ', 1), (' C ', 3), (' d ', 4)]
>>>l.sort (Lambda x,y:cmp (x[1],y[1))
>> >l
>>>[(' A ', 1), (' C ', 3), (' d ', 4), (' B ', 6)]
Instance 4: Sort the second keyword
>>>l = [(' B ', 6), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>l.sort (key=lambda x:x[1])
>>>l
>>>[(' A ', 1), (' C ', 3), (' d ', 4), (' B ', 6)]
Instance 5: Sort the second keyword
>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>import operator
>>>l.sort (key= Operator.itemgetter (1))
>>>l
>>>[(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)]
Example 6: (DSU method: Decorate-sort-undercorate)
>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]
>>>a = [(x[1],i,x) for i,x in Enumerate (L)] #i can confirm The stable sort
>>>a.sort ()
>>>l = [s[2] for S in A]
>>>l >>>[
(' a ' , 1), (' B ', 2), (' C ', 3), (' d ', 4)]
The above gives a method of sorting list in 6, where instance 3.4.5.6 can play a pair of items in the list item
Sorts the comparison keywords.
Efficiency comparison:
CMP < DSU < key
By experiment comparison, Method 3 is slower than method 6, method 6 is slower than Method 4, Method 4 and Method 5 are basically equivalent
Multi-keyword comparison sort:
Example 7:
>>>l = [(' d ', 2), (' A ', 4), (' B ', 3), (' C ', 2)]
>>> l.sort (Key=lambda x:x[1])
>>> L
>>>[(' d ', 2), (' C ', 2), (' B ', 3), (' A ', 4)]
We see that at this time the ordered L is just sorted by the second keyword,
What if we want to order the second keyword and then sort it with the first keyword? There are two ways
Example 8:
>>> L = [(' d ', 2], (' A ', 4), (' B ', 3), (' C ', 2)]
>>> l.sort (Key=lambda x: (x[1],x[0)))
>> > L
>>>[(' C ', 2), (' d ', 2), (' B ', 3), (' A ', 4)]
Example 9:
>>> L = [(' d ', 2], (' A ', 4), (' B ', 3), (' C ', 2)]
>>> l.sort (Key=operator.itemgetter (1,0))
> >> L
>>>[(' C ', 2), (' d ', 2), (' B ', 3), (' A ', 4)]
#实例8能够工作呢. The reason is that tuple is compared from left to right, compares the first, if equal, compares the second