Re: test for list equality
On Fri, Dec 16, 2011 at 12:11 AM, Ian Kelly wrote: > On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
Just for fun, use the Hungarian Algorithm (Python implementation: http://software.clapper.org/munkres/) On Fri, Dec 16, 2011 at 3: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! > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
On Thu, Dec 15, 2011 at 11:07 AM, noydb 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). -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
> > set(x) == set(y) > > Duplicates cause issues in the set() version: You're right, I stand corrected. -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
On 15/12/2011 17:49, noydb wrote: 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 ??? .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 >>> 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! :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
On Dec 15, 11:59 am, 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) 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
Ahh, I see (on the sort issue), thanks All! Still, any other slicker ways to do this? Just for learning. -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
On Thu, Dec 15, 2011 at 9:57 AM, John Gordon wrote: > In <61edc02c-4f86-45ef-82a1-61c701300...@t38g2000yqe.googlegroups.com> > noydb 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 . > And you cannot use the method in-line - it mutates the list in place, returning None. If you either do not wish to mutate the list, or you absolutely want to do the sort in-line, you need to use the sorted built-in: if sorted(x) == sorted(y): ... However, this will, temporary, use double the memory. > > -- > John Gordon A is for Amy, who fell down the stairs > gor...@panix.com B is for Basil, assaulted by bears >-- Edward Gorey, "The Gashlycrumb Tinies" > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
> 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) -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
In <61edc02c-4f86-45ef-82a1-61c701300...@t38g2000yqe.googlegroups.com> noydb 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 gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
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 ??? -- http://mail.python.org/mailman/listinfo/python-list
test for list equality
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