Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17284 > unrolled thread
| Started by | noydb <jenn.duerr@gmail.com> |
|---|---|
| First post | 2011-12-15 08:36 -0800 |
| Last post | 2011-12-16 00:32 -0700 |
| Articles | 19 — 10 participants |
Back to article view | Back to comp.lang.python
test for list equality noydb <jenn.duerr@gmail.com> - 2011-12-15 08:36 -0800
Re: test for list equality noydb <jenn.duerr@gmail.com> - 2011-12-15 09:49 -0800
Re: test for list equality John Gordon <gordon@panix.com> - 2011-12-15 17:57 +0000
Re: test for list equality Miki Tebeka <miki.tebeka@gmail.com> - 2011-12-15 09:59 -0800
Re: test for list equality MRAB <python@mrabarnett.plus.com> - 2011-12-15 18:12 +0000
Re: test for list equality darnold <darnold992000@yahoo.com> - 2011-12-15 10:12 -0800
Re: test for list equality Tim Chase <python.list@tim.thechases.com> - 2011-12-15 12:20 -0600
Re: test for list equality Miki Tebeka <miki.tebeka@gmail.com> - 2011-12-15 10:25 -0800
Re: test for list equality Miki Tebeka <miki.tebeka@gmail.com> - 2011-12-15 10:25 -0800
Re: test for list equality Terry Reedy <tjreedy@udel.edu> - 2011-12-15 20:02 -0500
Re: test for list equality Stefan Behnel <stefan_ml@behnel.de> - 2011-12-15 19:01 +0100
Re: test for list equality noydb <jenn.duerr@gmail.com> - 2011-12-15 10:07 -0800
Re: test for list equality Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-15 11:28 -0700
Re: test for list equality noydb <jenn.duerr@gmail.com> - 2011-12-15 10:32 -0800
Re: test for list equality MRAB <python@mrabarnett.plus.com> - 2011-12-15 18:06 +0000
Re: test for list equality MRAB <python@mrabarnett.plus.com> - 2011-12-15 17:54 +0000
Re: test for list equality Alec Taylor <alec.taylor6@gmail.com> - 2011-12-16 17:30 +1100
Re: test for list equality Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-16 00:11 -0700
Re: test for list equality Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-16 00:32 -0700
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-12-15 08:36 -0800 |
| Subject | test for list equality |
| Message-ID | <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-12-15 09:49 -0800 |
| Message-ID | <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> |
| In reply to | #17284 |
On Dec 15, 11:36 am, noydb <jenn.du...@gmail.com> wrote: > 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 >>> if x.sort == y.sort: ... print 'equal' ... else: ... print 'not equal' ... not equal ???
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-12-15 17:57 +0000 |
| Message-ID | <jcdcde$kpv$1@reader1.panix.com> |
| In reply to | #17285 |
In <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> noydb <jenn.duerr@gmail.com> writes:
> My sort issue... as in this doesn't work
> >>> if x.sort =3D=3D y.sort:
> ... print 'equal'
> ... else:
> ... print 'not equal'
> ...
> not equal
> ???
Use x.sort() instead of x.sort .
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2011-12-15 09:59 -0800 |
| Message-ID | <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> |
| In reply to | #17285 |
> My sort issue... as in this doesn't work
> >>> if x.sort == y.sort:
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)
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-12-15 18:12 +0000 |
| Message-ID | <mailman.3684.1323972728.27778.python-list@python.org> |
| In reply to | #17288 |
On 15/12/2011 17:59, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >> >>> if x.sort == y.sort: > 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.
[toc] | [prev] | [next] | [standalone]
| From | darnold <darnold992000@yahoo.com> |
|---|---|
| Date | 2011-12-15 10:12 -0800 |
| Message-ID | <a17c8ff7-eb8f-404a-8468-aa496db32f98@h4g2000yqk.googlegroups.com> |
| In reply to | #17288 |
On Dec 15, 11:59 am, Miki Tebeka <miki.teb...@gmail.com> wrote: > > My sort issue... as in this doesn't work > > >>> if x.sort == y.sort: > > 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) I'm pretty sure we don't want to use set() since it throws away duplicates: >>> x = [1,2,3,4] >>> y = [1,1,2,2,3,3,4] >>> sorted(x) == sorted(y) False >>> set(x) == set(y) True
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2011-12-15 12:20 -0600 |
| Message-ID | <mailman.3685.1323973214.27778.python-list@python.org> |
| In reply to | #17288 |
On 12/15/11 11:59, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >>>>> if x.sort == y.sort: > 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
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2011-12-15 10:25 -0800 |
| Message-ID | <mailman.3687.1323973544.27778.python-list@python.org> |
| In reply to | #17297 |
> > set(x) == set(y) > > Duplicates cause issues in the set() version: You're right, I stand corrected.
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2011-12-15 10:25 -0800 |
| Message-ID | <30504278.623.1323973542217.JavaMail.geo-discussion-forums@yqbo42> |
| In reply to | #17297 |
> > set(x) == set(y) > > Duplicates cause issues in the set() version: You're right, I stand corrected.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-12-15 20:02 -0500 |
| Message-ID | <mailman.3709.1323997509.27778.python-list@python.org> |
| In reply to | #17288 |
On 12/15/2011 12:59 PM, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >>>>> if x.sort == y.sort: > 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 -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2011-12-15 19:01 +0100 |
| Message-ID | <mailman.3679.1323972097.27778.python-list@python.org> |
| In reply to | #17285 |
noydb, 15.12.2011 18:49: > On Dec 15, 11:36 am, noydb wrote: >> 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 > >>> if x.sort == y.sort: > ... 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
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-12-15 10:07 -0800 |
| Message-ID | <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> |
| In reply to | #17289 |
Ahh, I see (on the sort issue), thanks All! Still, any other slicker ways to do this? Just for learning.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-12-15 11:28 -0700 |
| Message-ID | <mailman.3688.1323973733.27778.python-list@python.org> |
| In reply to | #17293 |
On Thu, Dec 15, 2011 at 11:07 AM, noydb <jenn.duerr@gmail.com> wrote: > 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).
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-12-15 10:32 -0800 |
| Message-ID | <60b72ba6-5456-4e8a-99f2-da2879641f70@r28g2000yqj.googlegroups.com> |
| In reply to | #17300 |
Thanks All, Thanks MRAB -- good note on the sort() vs sorted().
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-12-15 18:06 +0000 |
| Message-ID | <mailman.3683.1323972336.27778.python-list@python.org> |
| In reply to | #17285 |
On 15/12/2011 17:49, noydb wrote: > On Dec 15, 11:36 am, noydb<jenn.du...@gmail.com> wrote: >> 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 >>>> if x.sort == y.sort: > ... 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: >>> x [2, 5, 1, 88, 9] >>> x.sort <built-in method sort of list object at 0x00A58508> >>> x.sort() >>> x [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! :-)
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-12-15 17:54 +0000 |
| Message-ID | <mailman.3678.1323971638.27778.python-list@python.org> |
| In reply to | #17284 |
On 15/12/2011 16:36, noydb wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Alec Taylor <alec.taylor6@gmail.com> |
|---|---|
| Date | 2011-12-16 17:30 +1100 |
| Message-ID | <mailman.3716.1324017045.27778.python-list@python.org> |
| In reply to | #17284 |
Just for fun, use the Hungarian Algorithm (Python implementation: http://software.clapper.org/munkres/) On Fri, Dec 16, 2011 at 3:36 AM, noydb <jenn.duerr@gmail.com> wrote: > 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! > -- > http://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-12-16 00:11 -0700 |
| Message-ID | <mailman.3717.1324019503.27778.python-list@python.org> |
| In reply to | #17284 |
On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor <alec.taylor6@gmail.com> wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-12-16 00:32 -0700 |
| Message-ID | <mailman.3718.1324020764.27778.python-list@python.org> |
| In reply to | #17284 |
On Fri, Dec 16, 2011 at 12:11 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor <alec.taylor6@gmail.com> wrote:
>> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web