3.9. order_by_index()
¶
-
natsort.
order_by_index
(seq, index, iter=False)¶ Order a given sequence by an index sequence.
The output of index_natsorted and index_versorted is a sequence of integers (index) that correspond to how its input sequence would be sorted. The idea is that this index can be used to reorder multiple sequences by the sorted order of the first sequence. This function is a convenient wrapper to apply this ordering to a sequence.
Parameters: seq : iterable
The sequence to order.
index : iterable
The sequence that indicates how to order seq. It should be the same length as seq and consist of integers only.
iter : {True, False}, optional
If True, the ordered sequence is returned as a generator expression; otherwise it is returned as a list. The default is False.
Returns: out : {list, generator}
The sequence ordered by index, as a list or as a generator expression (depending on the value of iter).
See also
Examples
order_by_index is a comvenience function that helps you apply the result of index_natsorted or index_versorted:
>>> a = ['num3', 'num5', 'num2'] >>> b = ['foo', 'bar', 'baz'] >>> index = index_natsorted(a) >>> index [2, 0, 1] >>> # Sort both lists by the sort order of a >>> order_by_index(a, index) [u'num2', u'num3', u'num5'] >>> order_by_index(b, index) [u'baz', u'foo', u'bar']