The contents of Sort HOW TO — Python 3.5.1 Document and additional tips.
list.sort()
--Sort the list and replace the original list. --There is no return value.
>>> a = [3, 0, 5, 1]
>>> a.sort()
>>> a
[0, 1, 3, 5]
>>> print(a.sort()) #There is no return value
None
sorted(list)
--Returns the result of sorting the list. --The original list is unchanged. --Can be used for other than lists.
>>> a = [3, 0, 5, 1]
>>> sorted(a)
[0, 1, 3, 5]
>>> a #The original list remains unchanged
[3, 0, 5, 1]
>>> sorted(iter([3, 0, 5, 1])) #Can be used for other than lists
[0, 1, 3, 5]
--Specify reverse = True
.
>>> a = [3, 0, 5, 1]
>>> sorted(a, reverse=True)
[5, 3, 1, 0]
--Specify a function or lambda expression in key
.
>>> a = ["c", "aaa", "Bb"]
>>> sorted(a, key=lambda x: x.lower())
['aaa', 'Bb', 'c']
>>> sorted(a, key=str.lower)
['aaa', 'Bb', 'c']
>>> sorted(a, key=len)
['c', 'Bb', 'aaa']
--By default, the elements are compared in order.
python
>>> a = [("b", 3), ("b", 1), ("a", 1), ("a", 2), ("c", 2)]
>>> sorted(a)
[('a', 1), ('a', 2), ('b', 1), ('b', 3), ('c', 2)]
--When specifying a key, using itemgetter is more concise than a lambda expression.
python
>>> a = [{"key1": 3}, {"key1": 1}]
>>> from operator import itemgetter
>>> sorted(a, key=itemgetter("key1"))
[{'key1': 1}, {'key1': 3}]
--When specifying a key, using attr getter is more concise than a lambda expression.
python
>>> from datetime import date
>>> a = [date(2016, 1, 1), date(2015, 12, 1)]
>>> from operator import attrgetter
>>> sorted(a, key=attrgetter("month"))
[datetime.date(2016, 1, 1), datetime.date(2015, 12, 1)]
--Composite sorting is possible by further sorting the sort results. --Sort the keys with the lowest priority first.
python
>>> a = [("b", 3), ("b", 1), ("a", 1), ("a", 2), ("c", 2)]
>>> b = sorted(a, key=itemgetter(1), reverse=True)
>>> sorted(b, key=itemgetter(0))
[('a', 2), ('a', 1), ('b', 3), ('b', 1), ('c', 2)]
--Itemgetter and attrgetter can specify multiple keys at once, so use this if the descending and ascending order are the same for all keys.
python
>>> sorted(a, key=itemgetter(0, 1))
[('a', 1), ('a', 2), ('b', 1), ('b', 3), ('c', 2)]
--If you sort normally, it will fail. --As a compound sort, specify Bool as None in the first key and the element itself in the second key.
>>> a = [3, 0, None, 5, 1]
>>> sorted(a) #Sorting normally fails
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()
>>> sorted(a, key=lambda x: (x is None, x)) #None at the end
[0, 1, 3, 5, None]
>>> sorted(a, key=lambda x: (x is not None, x)) #None at the beginning
[None, 0, 1, 3, 5]
Python sort list with None at the end - Stack Overflow
--Implement __lt__ ()
.
class P(object):
def __init__(self, gender, age):
self.gender = gender
self.age = age
def __repr__(self):
return "{:s}{:d}".format(self.gender, self.age)
def __lt__(self, other):
return (self.gender, self.age) < (other.gender, other.age)
a = [P("F", 30), P("F", 20), P("M", 20), P("M", 40), P("F", 10)]
print(sorted(a))
-Use heapq.merge.
>>> import heapq
>>> list(heapq.merge([1, 3, 4, 7], [2, 5], [6]))
[1, 2, 3, 4, 5, 6, 7]
-Sort large text files in Python --Qiita
Recommended Posts