Brian Blais wrote: > Hello, > > I have two lists, one with strings (filenames, actually), and one with a > real-number > rank, like: > > A=['hello','there','this','that'] > B=[3,4,2,5] > > I'd like to sort list A using the values from B, so the result would be > in this example, > > A=['this','hello','there','that']
Here's a solution that makes use of the key= argument to sorted(): >>> A = ['hello','there','this','that'] >>> B = [3,4,2,5] >>> indices = range(len(A)) >>> indices.sort(key=B.__getitem__) >>> [A[i] for i in indices] ['this', 'hello', 'there', 'that'] Basically, it sorts the indices to A -- [0, 1, 2, 3] -- in the order given by B, and then selects the items from A in the appropriate order. -- http://mail.python.org/mailman/listinfo/python-list