Consider a Python list my_list
containing ['foo', 'foo', 'bar']
.
What is the most Pythonic way to uniquify and sort a list ?
(think cat my_list | sort | uniq
)
This is how I currently do it and while it works I'm sure there are better ways to do it.
my_list = [] ... my_list.append("foo") my_list.append("foo") my_list.append("bar") ... my_list = set(my_list) my_list = list(my_list) my_list.sort()
Using Python's import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy. array(list) and then use numpy. unique(x) function to get the unique values from the list.
A best sorting algorithm in python The time complexity of quicksort is O(n log n) in the best case, O(n log n) in the average case, and O(n^2) in the worst case. Quicksort is also considered as the ” fastest” sorting algorithm because it has the best performance in the average case for most inputs.
Summary. Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.
The previous investigations showed us, that list. sort is slightly faster than sorted and consumes around 24% less memory. However, keep in mind that list. sort is only implemented for lists, whereas sorted accepts any iterable.
my_list = sorted(set(my_list))
# Python ≥ 2.4 # because of (generator expression) and itertools.groupby, sorted import itertools def sort_uniq(sequence): return (x[0] for x in itertools.groupby(sorted(sequence)))
Faster:
import itertools, operator import sys if sys.hexversion < 0x03000000: mapper= itertools.imap # 2.4 ≤ Python < 3 else: mapper= map # Python ≥ 3 def sort_uniq(sequence): return mapper( operator.itemgetter(0), itertools.groupby(sorted(sequence)))
Both versions return an generator, so you might want to supply the result to the list type:
sequence= list(sort_uniq(sequence))
Note that this will work with non-hashable items too:
>>> list(sort_uniq([[0],[1],[0]])) [[0], [1]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With