test for list equality

N

noydb

I want to test for equality between two lists. For example, if I have
two lists that are equal in content but not in order, I want a return
of 'equal' -- dont care if they are not in the same order. In order
to get that equality, would I have to sort both lists regardless? if
yes, how (having issues with list.sort)?

Another way i tried, that I think is kind-of roundabout is like
x = [2, 5, 1, 88, 9]
y = [5, 2, 9, 1, 88]
inBoth = list(set(x) & set(y))

and then test that list.count is equal between inBoth and x and/or y.


Any better suggestions?

Thanks for any help!
 
N

noydb

I want to test for equality between two lists.  For example, if I have
two lists that are equal in content but not in order, I want a return
of 'equal' -- dont care if they are not in the same order.  In order
to get that equality, would I have to sort both lists regardless?  if
yes, how (having issues with list.sort)?

Another way i tried, that I think is kind-of roundabout is like
x = [2, 5, 1, 88, 9]
y = [5, 2, 9, 1, 88]
inBoth = list(set(x) & set(y))

and then test that list.count is equal between inBoth and x and/or y.

Any better suggestions?

Thanks for any help!

My sort issue... as in this doesn't work.... print 'equal'
.... else:
.... print 'not equal'
....
not equal


???
 
M

MRAB

I want to test for equality between two lists. For example, if I have
two lists that are equal in content but not in order, I want a return
of 'equal' -- dont care if they are not in the same order. In order
to get that equality, would I have to sort both lists regardless? if
yes, how (having issues with list.sort)?

Another way i tried, that I think is kind-of roundabout is like
x = [2, 5, 1, 88, 9]
y = [5, 2, 9, 1, 88]
inBoth = list(set(x)& set(y))

and then test that list.count is equal between inBoth and x and/or y.


Any better suggestions?

Thanks for any help!

You could count the number of times each item occurs using the Counter
class in the collections module:
x = [2, 5, 1, 88, 9]
y = [5, 2, 9, 1, 88]
from collections import Counter
cx = Counter(x)
cy = Counter(y)
cx Counter({88: 1, 1: 1, 2: 1, 5: 1, 9: 1})
cx == cy
True
 
M

Miki Tebeka

My sort issue... as in this doesn't workYou're missing the () to make it a function call.
Also list.sort() returns none, it mutates the original list.
You can either
sorted(x) == sorted(y)
or
set(x) == set(y)
 
S

Stefan Behnel

noydb, 15.12.2011 18:49:
I want to test for equality between two lists. For example, if I have
two lists that are equal in content but not in order, I want a return
of 'equal' -- dont care if they are not in the same order. In order
to get that equality, would I have to sort both lists regardless? if
yes, how (having issues with list.sort)?

Another way i tried, that I think is kind-of roundabout is like
x = [2, 5, 1, 88, 9]
y = [5, 2, 9, 1, 88]
inBoth = list(set(x)& set(y))

and then test that list.count is equal between inBoth and x and/or y.

Any better suggestions?

Thanks for any help!

My sort issue... as in this doesn't work... print 'equal'
... else:
... print 'not equal'
...
not equal

alist.sort() is a method, so you have to call it in order to execute it.
alist.sort will only give you a reference to the method. Comparing that to
another method reference returns False, as expected.

Also, calling it does not return anything (useful), it modifies the list in
place.

If you want to create a new list (which you don't want in this case, but
anyway), you can use the sorted() builtin function.

Stefan
 
M

MRAB

I want to test for equality between two lists. For example, if I have
two lists that are equal in content but not in order, I want a return
of 'equal' -- dont care if they are not in the same order. In order
to get that equality, would I have to sort both lists regardless? if
yes, how (having issues with list.sort)?

Another way i tried, that I think is kind-of roundabout is like
x = [2, 5, 1, 88, 9]
y = [5, 2, 9, 1, 88]
inBoth = list(set(x)& set(y))

and then test that list.count is equal between inBoth and x and/or y.

Any better suggestions?

Thanks for any help!

My sort issue... as in this doesn't work... print 'equal'
... else:
... print 'not equal'
...
not equal


???

..sort is a method which sorts the list in-place and returns None. You
must provide the () if you want to call it, otherwise you just get a
reference to the method:
[1, 2, 5, 9, 88]

There's also a function "sorted" which returns its argument as a sorted
list. The argument itself isn't altered:
y = [5, 2, 9, 1, 88]
sorted(y) [1, 2, 5, 9, 88]
y
[5, 2, 9, 1, 88]

It's all in the documentation! :)
 
N

noydb

Ahh, I see (on the sort issue), thanks All!

Still, any other slicker ways to do this? Just for learning.
 
M

MRAB

You're missing the () to make it a function call.
Also list.sort() returns none, it mutates the original list.
You can either
sorted(x) == sorted(y)
or
set(x) == set(y)

But don't use sets if there may be duplicates.
 
T

Tim Chase

You're missing the () to make it a function call.
Also list.sort() returns none, it mutates the original list.
You can either
sorted(x) == sorted(y)
or
set(x) == set(y)

Duplicates cause issues in the set() version:

a = [1,2,3,4]
b = a + a
print sorted(a) == sorted(b) # False
print set(a) == set(b) # True

They mean different things, and the OP may want one or the other
depending on how they want to consider duplicates.

-tkc
 
I

Ian Kelly

Ahh, I see (on the sort issue), thanks All!

Still, any other slicker ways to do this?  Just for learning.

MRAB's collections.Counter suggestion is what I would do. Very tidy,
and also more efficient I think: O(n) instead of O(n log n).
 
T

Terry Reedy

You're missing the () to make it a function call.
Also list.sort() returns none, it mutates the original list.
You can either
sorted(x) == sorted(y)
or
set(x) == set(y)

or x.sort(); y.sort(); x == y
 
I

Ian Kelly

Just for fun, use the Hungarian Algorithm

(Python implementation: http://software.clapper.org/munkres/)

That's a pretty silly approach, but okay:

def listequals(a, b):
if len(a) != len(b):
return False
matrix = [[int(item_a != item_b) for item_b in b] for item_a in a]
path = Munkres().compute(matrix)
return sum(matrix[r][c] for (r, c) in path) == 0
 
I

Ian Kelly

Just for fun, use the Hungarian Algorithm

(Python implementation: http://software.clapper.org/munkres/)

That's a pretty silly approach, but okay:

def listequals(a, b):
   if len(a) != len(b):
       return False
   matrix = [[int(item_a != item_b) for item_b in b] for item_a in a]
   path = Munkres().compute(matrix)
   return sum(matrix[r][c] for (r, c) in path) == 0

Amendment -- it seems that Hungarian implementation fails on an empty matrix:

def listequals(a, b):
if len(a) == len(b) == 0:
return True
if len(a) != len(b):
return False
matrix = [[int(item_a != item_b) for item_b in b] for item_a in a]
path = Munkres().compute(matrix)
return sum(matrix[r][c] for (r, c) in path) == 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top