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 ', ' has ', ' 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 with the same meaning as those for the list.sort () &nbs P;method (described in section mutable Sequence Types).
CMP Specifies (specify) a custom comparison function of the arguments (iterable (iterative) elements) which should return a negativ E (plural), zero or positive (positive number) number depending on whether the first argument are considered smaller than, equal to, or larger than 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.lower . 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.
#字符串排序使用是字典序, rather than 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, ' + ': 0}print sorted (Dic.iteritems (), Key=lambda d:d[1], reverse = False)
#[(' d ', 0), (' C ', 3), (' ASD ', 4), (' BC ', 5), (' A ', 31), (' 33 ', 56)]
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# (' d ', 0) d 0# (' BC ', 5) BC AA (' @ ', ' 4 ') 56# (' ASD ', 4) ASD
(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).
Key = Lambda d:d[1]for i in Dic.iteritems (): print key (i), #输出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 ', ' ASD ', ']print ', ' d ' sorted (LIS) #[', ' a ', ' ASD ', ' BC ', ' C ', ' d ']
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
}]
|
Python dictionary sort about sort (), reversed (), sorted ()