On Mon, 11 Jan 2010 18:57:24 +0100, mk wrote: >> I have two lists of IP addresses: >> >> hostips = [ 'a', 'b', 'c', 'd', 'e' ] >> >> thread_results = [ 'd', 'b', 'c' ] >> >> I need to sort thread_results in the same order as hostips. > > P.S. One clarification: those lists are actually more complicated > (thread_result is a list of tuples (ip, thread)), which is why I need > thread_results sorted in order of hostips (instead of just constructing > [ h for h in hostips if h in thread_results ] and be done with it).
1. thread_results_dict = dict([(v[0], v) for v in thread_results]) [thread_results_dict[h] for h in hostips if h in thread_results_dict] 2. hostips_dict = dict([(ip, idx) for idx, ip in enumerate(hostips)]) sorted(thread_results, key = lambda r: hostips_dict[r[0]]) -- http://mail.python.org/mailman/listinfo/python-list