First, the sort of python
1, reversed ()
This is very well understood,reversed English means:adj. reversed; reverse; (judgment, etc) revoked
Print List (reversed (['dream','a','have','I')])# [' I ', ' have ', ' a ', ' dream ']
2. Confusing sort () with sorted ()
In Python sorted is the built-in function (BIF), and sort () is the built-in function list.sort () of the list type.
Sorted ()
-
- sorted ( iterable[, cmp[, key[, reverse]] [ ]
-
-
Return a new sorted list from the items in iterable.
The optional arguments (optional parameter) CMP, key, and reverse has the same meaning as those for the List.sort () method (des cribed in section Mutable Sequence Types).
CMP Specifies (Designation) a custom comparison function of the arguments (iterable (iterative) elements) which should return a negative (complex numbers), zero or positive (positive) number depending on whether the first argument are considered smaller than, equal to, or larger th An the second argument: cmp=lambda x,y:cmp (X.lower (), Y.lower ()). The default value is None.
key specifies a function of one argument that's used to extract a comparison key from each list element: Key=str.lowe R. The default value is None (compare the elements directly).
Reverse is a Boolean value. If set to True, then the list elements is sorted as if each comparison were reversed.
#String sorting is used in dictionary order, not alphabetical order"""Sorted () sorted by dictionary order"""Lis = [‘A‘,‘C‘,‘Z‘,‘E','T','C','b','A','good',' Tack']print sorted (LIS) #[' A ', ' C ', ' E ', ' good ', ' T ', ' Tack ', ' A ', ' B ', ' C ', ' Z ']
About Dictionary Ordering:
can refer to Baidu encyclopedia. Http://baike.baidu.com/view/4670107.htm
According to the ASCII row, the following:
0-9 (corresponds to the value 48-59);
A-Z (corresponds to the value 65-90);
A-Z (corresponds to the value 97-122);
------------
Standard order: Short in front, long in the back, and so on in turn than the letter,
such as to < up < Caps < Cat < too < <boat < boot < card
such as boat < boot <cap < card < Cat < to < too<, < up
What's more, dictionary order is the sort of dictionary, like a dictionary. I have not found the authority of the instructions, what is the dictionary order???? Ask for answers!!
Sort ()
S.sort ([cmp[, key[, reverse]])
Third, the dictionary sort of Python
1. Some features of the Python dictionary
Disordered:
Dictionary, shaped like dic = {' A ': 1, ' B ': 2, ' C ': 3}, the elements in the dictionary are not in order, so dic[0] is syntactically incorrect.
No weight:
There can be no duplicate key value, so dic.add[' c ' = 4, the dictionary becomes {' A ': 1, ' B ': 2, ' C ': 4}.
2. Sorting in different order according to "key" or "key value"
Function prototype: sorted (Dic,value,reverse)
Explanation:dic is a comparison function, value is a sorted object (this refers to a key or a key value),
Reverse: Indicate ascending or descending, true--descending, false--ascending (default)
3. Example:
DIC = {' A ': +, ' BC ': 5, ' C ': 3, ' ASD ': 4, ' + ': +, ' d ': 0}
The value of DIC is to be sorted from the big to the small (value is an integer).
DIC = {‘A ": " bc": 5, ' c " : 3, asd": 4, " 33
The explanations are as follows:
(1), Dic.iteritems (), returns the Ganso collection of dictionary key-value pairs
print dic.iteritems () # <dictionary-itemiterator object at 0x00b71a80>for obj in Dic.iteritems (): Print Obj,obj[0],obj[1] # (' a ', +) a 31# (' C ', 3) C 3#< Span style= "COLOR: #008000" > (' d ', 0) d 0# (' BC ', 5) BC 5# (' all ', ') 56# (' ASD ', 4) ASD 4
(2), about sort objects
As mentioned above, value (or key) is a sorted object (this refers to a key or a key value), but why use a lambda function, see here: Click to read
Key=lambda D:d[1] is the sort object with the key value (value).
Lambda d:d[1] in Dic.iteritems (): # output 31 3 0 5 56 4 These are the values of the dictionary dic
If you select key = Lambda d:d[0], select Key key as the sort object.
(3), reverse
Reverse is reversed, reverse=ture represents reverse.
(4), note:
Sorted (Dic.iteritems (), Key=lambda d:d[1], reverse = False) Iterates the dic.iteritems of each of the Ganso () key-value pairs, each of which passes as a parameter to key () function (I'm talking about this: Key=lambda d:d[1],).
4. Review
lis = [ ' a ", ' bc ", "c", d< Span style= "COLOR: #800000" > ' ]print sorted (LIS) #[', ' a ', ' ASD ', ' BC ', ' C ', ' d '] /span>
Compare letters, such as boat < boot <cap < card < Cat < to < too< and < up
5. Questions
Examples can be found in: [**python's sorting function sort,sorted application in list sorting and dictionary sorting and Examples *] (http://wangwei007.blog.51cto.com/68019/1100742)
There is now a sort of sort in this case. such as the big question number sort, then the big question corresponding small question number also sorts, as follows:
1234567891011121314151617181920212223 |
lis
= [{
‘Big‘
:
3
,
‘small‘
:
2
},{
‘Big‘
:
3
,
‘small‘
:
4
},{
‘Big‘
:
2
,
‘small‘
:
2
}, {
‘Big‘
:
3
,
‘small‘
:
1
},{
‘Big‘
:
2
,
‘small‘
:
1
},{
‘Big‘
:
1
,
‘small‘
:
1
}]
# 大题号排序
li
= sorted
(lis, key
=
lambda s: s[
‘Big‘
])
# 输出:
#[{‘small‘: 1, ‘Big‘: 1}, {‘small‘: 2, ‘Big‘: 2}, {‘small‘: 1, ‘Big‘: 2}, {‘small‘: 2, ‘Big‘: 3}, {‘s
mall
‘: 4, ‘
Big
‘: 3}, {‘
small
‘: 1, ‘
Big‘:
3
}]
# 小题号排序:
sort_ff
= []
no
= set
([i[
‘Big‘
]
for i
in li])
for obj
in no:
li_
= []
for i
in ff:
if i[
‘Big‘
]
=
= obj:
li_.append(i)
l
= sorted
(li_, key
=
lambda s: s[
‘small‘
])
for j
in l:
sort_ff.append(j) # 输出结果:
[{
‘small‘
:
1
,
‘Big‘
:
1
}, {
‘small‘
:
1
,
‘Big‘
:
2
}, {
‘small‘
:
2
,
‘Big‘
:
2
}, {
‘small‘
:
1
,
‘Big‘
:
3
}, {
‘small‘
:
2
,
‘Big‘
:
3
}, {
‘small‘
:
4
,
‘Big‘
:
3
}]
|
Use sort () or sorted (), A.sort () has changed its structure, B = A.sort () is the wrong way to do it! and sorted (A, ...) Does not change the structure of a.
< turn >python dictionary sort about sort (), reversed (), sorted ()