Interesting (?) problem

Arnaud Delobelle arnodel at googlemail.com
Mon Jan 11 13:17:38 EST 2010


mk <mrkafk at gmail.com> writes:

> 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.

[...solution:]
> hostips_limited = []
> for h in hostips:
>     if h in thread_results:
>         hostips_limited.append(h)
>
> ..and then doing sorting thread_results.

What do you mean by that last sentence?

[... or:]
> Incidentally, it *seems* that list comprehension preserves order:
>
> hostips_limited = [ h for h in hostips if h in thread_results ]

That's ok, but why not keep thread_results as a set instead of a list if
the ordering in that container is not significant but you are testing
membership a lot?

-- 
Arnaud



More information about the Python-list mailing list