The reason may be the version problem of Python 2 and 3
chestnut 1: call sorted() and pass in reversed()_ CMP can achieve reverse order sorting
def reversed_cmp(x, y):
if x > y:
return 1
if x < y:
return -1
return 0
L1 = [16, 5, 120, 9, 66]
print(sorted(L1, reversed_cmp))
terms of settlement:
L1 = [16, 5, 120, 9, 66]
print(sorted(L1, reverse=True))
The results were as follows
[120, 66, 16, 9, 5]
Maybe it’s too simple. Python doesn’t bother to use custom functions…
in continuous update...
Chestnut 2: using sorted() higher-order function to realize the algorithm of ignoring case sorting
def cmp_ignore_case(s1, s2):
if s1[0].lower() > s2[0].lower():
return -1
if s1[0].lower() < s2[0].lower():
return 1
return 0
print(sorted(['haha', 'about', 'TIM', 'Credit'], cmp_ignore_case))
terms of settlement:
def com_flag(s):
return s.lower()
# key represents the key function, the default is None, reverse represents whether to reverse the order, the default is False
# The following functions are arranged in reverse order
print(sorted(['haha', 'about', 'TIM', 'Credit'], key=com_flag, reverse=True))
The results were as follows
['Zoo', 'Credit', 'bob', 'about']
come on.
Read More:
- [How to Fix]Type error: must use keyword argument for key function
- Libtorch Error: Expected object of type Variable but found type CUDALongType for argument #2 ‘index’
- tf.nn.dilation2d Call Error: TypeError: dilation2d_v1() got an unexpected keyword argument ‘data_format’
- [Solved] Error ‘false‘ undeclared (first use in this function)
- [Go] Solve the empty interface interface{} cannot use (type []string) as type []interface {}
- C++ Use of deleted function error
- .NETproject compilation error. Type or namespace name could not be found. Visual studio automatically introduces dependency package (shortcut key)
- Linux C++ Error: invalid use of incomplete type [How to Solve]
- Taro Use React Hooks Error: TypeError: Object(…) is not a function
- R language Use setwd() function Error [How to Solve]
- QT solves error: use of deleted function and is private within this context
- E11000 duplicate key error collection: *** index: _id_ dup key
- [Solved] Gitee generates public key error: Enter file in which to save the key…
- [Solved] SecureCRT Error: Key exchange failed. No compatible key exchange method.
- [Solved] Parsing error: The keyword ‘import‘ is reserved
- How to Solve ES error: “illegal_argument_exception”
- [Solved] logback log format adjustment error: Failed to create converter for [%M] keyword
- [Solved] Vscode Katex Error: ParseError: KaTeX Parse Error: Can‘t Use Function ‘$‘ In Math Mode At Position …
- [Solved] Error while extracting response for type [] and content type []…