Interesting (?) problem
Nobody
nobody at nowhere.com
Mon Jan 11 14:10:30 EST 2010
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]])
More information about the Python-list
mailing list