0
What is the purpose of key=None, how to use key ? What can we place in place of None ?
I know that we can sort alphabetically/numerically in ascending or descending order by putting reverse = True or False list.sort(key=None, reverse=False)
8 Respostas
+ 7
Here are a couple. You can use key= in both sorts, i.e. lst.sort(key=) and sorted(lst, key=)
https://code.sololearn.com/cwB76GNUFftP
The sort method sorts the list in place. The sorted function returns a sorted copy of the list.
lst1 = [1, 3, 2]
lst1.sort()
print(lst1) # output [1, 2, 3]
lst1 = [1, 3, 2]
print(sorted(lst1)) # output [1, 2, 3]
+ 3
l = ["first", "second", "third", "fourth", "fifth"]
l.sort(key=len)
# sort by length
print(l)
+ 2
At the bottom of the sort() doc there was a link that would have taken you to the "How To" area that would have described it further with examples. Here's that link.
https://docs.python.org/3/howto/sorting.html#sortinghowto
+ 2
Here's a more detailed explanation
https://code.sololearn.com/cHAqka9YOVph
+ 1
Read the section for list.sort() in the docs. It describes what the key is for and reverse is kinda obvious, but it is also described.
https://docs.python.org/3/library/stdtypes.html#list.sort
0
assuming you are talking of function signature:
if no values provided for 'key' and 'reverse' argument, default to 'None' (alphabetical sort) and 'False' (ascending order)
key is used to provide a sort function wich overide the default one...
0
ChaoticDawg there is no example provided in library/stdtypes, please provide me some example.
- 1
Привет здесь есть русские