473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Best way to compare a list?

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
Jul 18 '05 #1
6 2195
>>>>> "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

Jul 18 '05 #2
fa****@jaredweb.com (Fazer) wrote in message news:<7b**************************@posting.google. com>...
Hello,

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


To compare a list to _what_?
Jul 18 '05 #3
Hello Faizan,
What do you guys think? The best/fastest way of comparing lists.

l1 = range(10)
l2 = range(10)
l1 == l2

True

HTH.
Miki
Jul 18 '05 #4
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,
Jul 18 '05 #5
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
Jul 18 '05 #6
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

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robin Siebler | last post by:
I have two directory trees that I want to compare and I'm trying to figure out what the best way of doing this would be. I am using walk to get a list of all of the files in each directory. I...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
5
by: yvan | last post by:
Approximately once a month, a client of ours sends us a bunch of comma-delimited text files which I have to clean up and then import into their MS SQL database. All last week, I was using a Cold...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
4
by: Guy Noir | last post by:
Hello. Is there a pattern or best practice for the following scenario? I have a list of items I would like to compare. The number of items are decided at runtime. ObjectA, ObjectB,...
21
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list...
26
by: puzzlecracker | last post by:
It'd be interesting to compare the learning practices of c++ practitioners. I'll start with mine The C++ Programming Language C++ Primer Effective C++ More Effective C++ Effective STL The...
5
by: antani | last post by:
I need to implement a function with a argument that is a compare function. This compare function must be several for every necessity. For example , I would like a compare function to analyze...
4
Niheel
by: Niheel | last post by:
http://bytes.com/images/howtos/career_opps_online_profswanted.jpgFor tech professionals, the best opportunities are being posted on online job boards. In a recent survey of 1,000 HR professionals...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.