Interesting (?) problem
Jean-Michel Pichavant
jeanmichel at sequans.com
Mon Jan 11 13:36:05 EST 2010
mk wrote:
> mk wrote:
>> Hello everyone,
>>
>> 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).
>
>
>
>
Could be easily done by changing the thread_results structure. You seems
to want to lookup using ips:
thread_results = {'ip1':['t1','t2','t3'], 'ip3':['t4','t8']} # tx are
threads
results = []
for ip in hostips:
results += [(ip, thread) for thread in thread_results.get(ip, [])]
>>> print results
Out[10]: [('ip1', 't1'), ('ip1', 't2'), ('ip1', 't3'), ('ip3', 't4'),
('ip3', 't8')]
In a general manner, if you end up with duplicated informations inside
your structures (like ips in you thread_results structure) that means
you'll need sooner or later to do additional loop work to factorize the
data.
JM
More information about the Python-list
mailing list