Fazer wrote:
John Hunter <jd******@ace.bsd.uchicago.edu> wrote in message news:<ma**************************************@pyt hon.org>...
>>>"Fazer" == Fazer <fa****@jaredweb.com> writes:
Fazer> Hello, I was wondering which would be the best way to
Fazer> compare a list?
Fazer> I was thinking of just using a for loop and testing the
Fazer> condition.
Fazer> What do you guys think? The best/fastest way of comparing
Fazer> lists.
What do you want to compare for, equality of all elements? Give a
little more info about what you need to do.
JDH
Sorry about that.
Basically, I have to lists. I have to compare them and pick out the difference(s).
Maybe the simple/fast approach would be to use a for loop?
Thanks,
If position information matters to you, I would imagine your initial
code is something like this...
import itertools
def list_differences(a, b):
l = [(x,y) for x,y in itertools.izip(a,b) if x != y]
if len(a) != len(b):
d = abs(len(a)-len(b))
if len(a) < len(b):
l.extend(zip(d*[None], b[len(a):]))
else:
l.extend(zip(a[len(b):], d*[None]))
return l
If position information matters, the above is pretty much optimal.
There doesn't seem to be a method in Python that would further speed up
iterating through two lists.
If positional information doesn't matter, and you just care about
whether or not objects exist in one, the other, or both lists, then I
believe this is easily done with Sets in Python 2.3. I don't know for
sure, I haven't used them yet.
- Josiah