GoldenEye: Source Forums

Debriefing => Off-Topic Lounge => Topic started by: Troy on October 06, 2012, 03:24:31 am

Title: Python compare lists
Post by: Troy on October 06, 2012, 03:24:31 am
I was doing some research about comparing lists in Python.  I came across the compare function.

http://www.tutorialspoint.com/python/list_cmp.htm

If I wanted to check if two lists had the same elements, is there a difference between:

Code: [Select]
if list1 == list2:
          ...

and

Code: [Select]
if cmp( list1, list2 ) == 0
          ...

I know there is a difference in other languages, but I do not know if Python is one of them.
Title: Re: Python compare lists
Post by: killermonkey on October 06, 2012, 02:03:06 pm
cmp( x, y ) returns -1, 0, or 1

x == y returns True or False

The best way to determine your answer is to just create simple test cases and find out for yourself.

Not sure why you would say "there is a difference in other languages" because cmp(...) is a built-in function to Python and unique to Python. Of course there will be differences.
Title: Re: Python compare lists
Post by: Troy on October 06, 2012, 05:27:41 pm
cmp( x, y ) returns -1, 0, or 1

x == y returns True or False

Yes, I know this.

The best way to determine your answer is to just create simple test cases and find out for yourself.

I ran a few tests in Idle and couldn't find a difference.  I searched the web, and couldn't find a clear answer either.  I thought I'd ask here.

Not sure why you would say "there is a difference in other languages" because cmp(...) is a built-in function to Python and unique to Python. Of course there will be differences.

Java: http://www.coderanch.com/t/409507/java/java/Difference-between-equals
Title: Re: Python compare lists
Post by: Mangley on October 06, 2012, 06:10:19 pm
I'd consider cmp to be the better of the two options. More versatile. You never know what extra functionality you might need later!

But I'm really a sucker for ==. It is elegant in it's simplicity.

cmp
Title: Re: Python compare lists
Post by: killermonkey on October 06, 2012, 06:54:05 pm
You cannot, and should not, compare Python to Java EVER. They have completely different ways of approaching variables, classes, and pretty much everything else.

Python "variables" act more like nametags. They are not even considered pointers, in the classical definition. Python runs exclusively on a dictionary based mentality, that can be seen clearly by calling any object's dict attribute:

Code: [Select]
print someobject.__dict__

Java runs a pure pointer approach. Each variable is exactly equivalent to a C++ pointer. This is why == in Java does not return true, also Java is a typed language meaning Integer cannot ever be String once it is defined in the code.

However, Python classes can override the __eq__ function to override what == means between ANY two objects. This means that list.__eq__ may be very different from dict.__eq__.

A good discussion to some of these principles: http://me.veekun.com/blog/2012/05/23/python-faq-passing/
Title: Re: Python compare lists
Post by: WNxEuphonic on October 06, 2012, 08:23:41 pm
cmp(a, b) returns more information than a==b, since it will return either 1 for a > b, 0 for a == b, or -1 for a < b while a==b returns a simple True/False.

cmp(1,2) returns -1
cmp(2,2) returns 0
cmp(2,1) returns 1

For lists, python compares each indice one by one until one pair is inequal or one of the lists runs out of indices.

cmp( [1, 1, 2], [1, 2, 0] ) returns -1; cmp( [2, 2], [2, 2, 2] ) returns -1
cmp( [1, 2, 1], [1 ,2, 1] ) returns 0
cmp( [1, 3, 1], [1, 1, 3] ) returns 1; cmp( [1], [0, 100, 100] ) returns 1; cmp( [1, 0, 0], [1] ) returns 1

So while you can use cmp(a,b) == 0 in place of a==b (since both will give True for equal, False for unequal) , cmp also gives you the ability to see which is larger.

---

Summary: cmp( a, b) == 0 will always give the same answer as a == b, but cmp(a,b) itself can give more information.
Title: Re: Python compare lists
Post by: Troy on October 06, 2012, 08:55:17 pm
Thanks, that's good information to know.  Looks like I won't have to edit Arsenal.