GoldenEye: Source Forums

  • November 16, 2024, 12:24:44 am
  • Welcome, Guest
Advanced search  

News:

Pages: [1]   Go Down

Author Topic: Python compare lists  (Read 11289 times)

0 Members and 1 Guest are viewing this topic.

Troy

  • GE:S Coder
  • 00 Agent
  • ***
  • Posts: 821
  • Reputation Power: 260
  • Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!
  • Offline Offline
Python compare lists
« 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.
« Last Edit: October 06, 2012, 04:53:43 am by Troy »
Logged
Complete - Arsenal, One Bullet is Enough, Tournament DM v2, TurboDM
Defunct - Agent Under Fire
VC - Being such a dick, KM must be stroked before springing into action.

killermonkey

  • GES Programmer
  • Retired Lead Developer
  • GE:S Fanatic
  • *
  • Posts: 5,473
  • Reputation Power: 346
  • killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!
  • Offline Offline
    • DroidMonkey Apps
Re: Python compare lists
« Reply #1 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.
« Last Edit: October 06, 2012, 02:08:02 pm by killermonkey »
Logged

Troy

  • GE:S Coder
  • 00 Agent
  • ***
  • Posts: 821
  • Reputation Power: 260
  • Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!
  • Offline Offline
Re: Python compare lists
« Reply #2 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
Logged
Complete - Arsenal, One Bullet is Enough, Tournament DM v2, TurboDM
Defunct - Agent Under Fire
VC - Being such a dick, KM must be stroked before springing into action.

Mangley

  • No Longer Leads The Art
  • Retired Lead Developer
  • 007
  • *
  • Posts: 1,848
  • Reputation Power: 270
  • Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!Mangley is awe-inspiring!
  • Offline Offline
Re: Python compare lists
« Reply #3 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
Logged
Concept Artist, Environment Artist, Effects Artist, Sound Designer

killermonkey

  • GES Programmer
  • Retired Lead Developer
  • GE:S Fanatic
  • *
  • Posts: 5,473
  • Reputation Power: 346
  • killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!killermonkey is awe-inspiring!
  • Offline Offline
    • DroidMonkey Apps
Re: Python compare lists
« Reply #4 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/
Logged

WNxEuphonic

  • 00 Agent
  • ***
  • Posts: 217
  • Reputation Power: 106
  • WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!WNxEuphonic is awe-inspiring!
  • Offline Offline
    • Euphonic.dev
Re: Python compare lists
« Reply #5 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.
« Last Edit: October 06, 2012, 08:36:41 pm by WNxEuphonic »
Logged

Troy

  • GE:S Coder
  • 00 Agent
  • ***
  • Posts: 821
  • Reputation Power: 260
  • Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!Troy is awe-inspiring!
  • Offline Offline
Re: Python compare lists
« Reply #6 on: October 06, 2012, 08:55:17 pm »

Thanks, that's good information to know.  Looks like I won't have to edit Arsenal.
Logged
Complete - Arsenal, One Bullet is Enough, Tournament DM v2, TurboDM
Defunct - Agent Under Fire
VC - Being such a dick, KM must be stroked before springing into action.
Pages: [1]   Go Up