Connecting Tech Pros Worldwide Help | Site Map

Best way to compare a list?

Fazer
Guest
 
Posts: n/a
#1: Jul 18 '05
Hello,

I was wondering which would be the best way to compare a list?

I was thinking of just using a for loop and testing the condition.

What do you guys think? The best/fastest way of comparing lists.

Thanks,

Faizan
John Hunter
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Best way to compare a list?


>>>>> "Fazer" == Fazer <faizan@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



Dan Bishop
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Best way to compare a list?


faizan@jaredweb.com (Fazer) wrote in message news:<7b454334.0401271739.59267018@posting.google. com>...[color=blue]
> Hello,
>
> I was wondering which would be the best way to compare a list?[/color]

To compare a list to _what_?
Miki Tebeka
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Best way to compare a list?


Hello Faizan,
[color=blue]
> What do you guys think? The best/fastest way of comparing lists.[/color]
[color=blue][color=green][color=darkred]
>>> l1 = range(10)
>>> l2 = range(10)
>>> l1 == l2[/color][/color][/color]
True

HTH.
Miki
Fazer
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Best way to compare a list?


John Hunter <jdhunter@ace.bsd.uchicago.edu> wrote in message news:<mailman.886.1075263140.12720.python-list@python.org>...[color=blue][color=green][color=darkred]
> >>>>> "Fazer" == Fazer <faizan@jaredweb.com> writes:[/color][/color]
>
> 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[/color]

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,
Josiah Carlson
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Best way to compare a list?


Fazer wrote:
[color=blue]
> John Hunter <jdhunter@ace.bsd.uchicago.edu> wrote in message news:<mailman.886.1075263140.12720.python-list@python.org>...
>[color=green][color=darkred]
>>>>>>>"Fazer" == Fazer <faizan@jaredweb.com> writes:[/color]
>>
>> 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[/color]
>
>
> 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,[/color]

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
Jeff Epler
Guest
 
Posts: n/a
#7: Jul 18 '05

re: Best way to compare a list?


What do you mean by a "difference"? For instance, what are the
differences between these two lists:

[1, 2, 3]
[3, 2, 1]

For some purposes you might want to consider them equivalent, since they
both contain 1, 2 and 3 (one time each) and they contain no other
elements. Or you might want to say that they differ at element 0 and
at element 2.

And for this list, what are the differences?

[1, 3, 4]
[1, 2, 3, 4]

For some purposes, you might say that they differ at indexes 1, 2, and
3. For other purposes, you might want to say that the second list has
an extra "2" inserted at position 1, but the lists are otherwise the
same.

Once you define what you mean by "differences", perhaps we can be more
helpful. But once you define exactly what your problem is, the
algorithm to solve it should be more apparent to you, too!

An answer to the simplest of the questions you may have been asking is
this:
[a == b for a, b in zip(l1, l2)]
this returns a list with True in the positions where corresponding
elements of l1 and l2 are equal, and False elsewhere.

Jeff

Closed Thread