Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > The best way to check if two lists have identical values

Reply
Thread Tools

The best way to check if two lists have identical values

 
 
mk
Guest
Posts: n/a
 
      02-25-2010
Hello everyone,

I have stumbled upon this seemingly trivial problem: the answer is not
there in http://www.python.org/doc/faq/programming/, and googling does
not return many references really (at least for me).

I have come up with this:

def qips_identical(q, oldq):
qips = map(operator.itemgetter(1), q)
oldqips = map(operator.itemgetter(1), oldq)
if len(qips) != len(oldqips):
return False
dif = set(qips) ^ set(oldqips)
if len(dif):
return False
return True

There's a number of complications here, depending on definition of
'lists with identical values', like whether the same value can be
repeated different number of times in two lists, or whether the order of
values matters.

In above example I assumed that the same values have to be repeated the
same numbers of times in both lists and that order doesn't matter so
long as the values are the same.

I was wondering if there's a better / shorter / faster way to do this --
not necessarily in the same variant, e.g. variant where order of two
lists matters would be interesting as well.

Regards,
mk

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-25-2010
mk wrote:

> I have stumbled upon this seemingly trivial problem: the answer is not
> there in http://www.python.org/doc/faq/programming/, and googling does
> not return many references really (at least for me).
>
> I have come up with this:
>
> def qips_identical(q, oldq):


> qips = map(operator.itemgetter(1), q)
> oldqips = map(operator.itemgetter(1), oldq)


I don't think the above is a relevant part of the problem.

> if len(qips) != len(oldqips):
> return False
> dif = set(qips) ^ set(oldqips)
> if len(dif):
> return False
> return True
>
> There's a number of complications here, depending on definition of
> 'lists with identical values', like whether the same value can be
> repeated different number of times in two lists, or whether the order of
> values matters.
>
> In above example I assumed that the same values have to be repeated the
> same numbers of times in both lists and that order doesn't matter so
> long as the values are the same.


No you haven't. [1, 1, 0] and [1, 0, 0] are considered equal by your
algorithm.

> I was wondering if there's a better / shorter / faster way to do this --
> not necessarily in the same variant, e.g. variant where order of two
> lists matters would be interesting as well.


sorted(left_list) == sorted(right_list)

should be good enough in most cases where you do care for repetitions but
not order.

Peter
 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      02-25-2010


mk wrote:
> Hello everyone,
>
> I have stumbled upon this seemingly trivial problem: the answer is not
> there in http://www.python.org/doc/faq/programming/, and googling does
> not return many references really (at least for me).
>
> I have come up with this:
>
> def qips_identical(q, oldq):
> qips = map(operator.itemgetter(1), q)
> oldqips = map(operator.itemgetter(1), oldq)
> if len(qips) != len(oldqips):
> return False
> dif = set(qips) ^ set(oldqips)
> if len(dif):
> return False
> return True
>
> There's a number of complications here, depending on definition of
> 'lists with identical values', like whether the same value can be
> repeated different number of times in two lists, or whether the order of
> values matters.
>
> In above example I assumed that the same values have to be repeated the
> same numbers of times in both lists and that order doesn't matter so
> long as the values are the same.


Your code checks if the two lists have the same length and the same
elements, but not necessarily the same number of each elements. E.g.

qips = [1, 1, 2]
oldqips = [1, 2, 2]

will return True

If you want to check if each value has the same number of occurences,
you can do

return sorted(qips) == sorted(oldqips)

assuming that all elements in the lists are comparable.

--
Arnaud
 
Reply With Quote
 
mk
Guest
Posts: n/a
 
      02-25-2010
On 2010-02-25 14:55, Arnaud Delobelle wrote:

> Your code checks if the two lists have the same length and the same
> elements, but not necessarily the same number of each elements. E.g.
>
> qips = [1, 1, 2]
> oldqips = [1, 2, 2]
>
> will return True
>
> If you want to check if each value has the same number of occurences,
> you can do
>
> return sorted(qips) == sorted(oldqips)
>
> assuming that all elements in the lists are comparable.


Thanks!

Regards,
mk

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-25-2010
On Thu, 25 Feb 2010 14:30:12 +0100, mk wrote:

> In above example I assumed that the same values have to be repeated the
> same numbers of times in both lists and that order doesn't matter so
> long as the values are the same.


sorted(list1) == sorted(list2)

> I was wondering if there's a better / shorter / faster way to do this --
> not necessarily in the same variant, e.g. variant where order of two
> lists matters would be interesting as well.


list1 == list2




--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Two identical Strings stored in two different object Neroku Java 12 02-12-2007 03:32 PM
I have two almost identical pages in IE, one works and the other doesn't Luminari HTML 23 02-11-2007 12:49 AM
I have two almost identical pages in IE, one works and the other doesn't Luminari Javascript 0 02-08-2007 09:52 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
form - check for identical values (password / email) =?Utf-8?B?WGF2aWVy?= ASP .Net 0 02-21-2006 06:04 PM



Advertisments