The best way to check if two lists have identical values

Discussion in 'Python' started by mk, Feb 25, 2010.

  1. mk

    mk Guest

    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
     
    mk, Feb 25, 2010
    #1
    1. Advertising

  2. mk

    Peter Otten Guest

    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
     
    Peter Otten, Feb 25, 2010
    #2
    1. Advertising

  3. 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
     
    Arnaud Delobelle, Feb 25, 2010
    #3
  4. mk

    mk Guest

    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
     
    mk, Feb 25, 2010
    #4
  5. 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
     
    Steven D'Aprano, Feb 25, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?WGF2aWVy?=

    form - check for identical values (password / email)

    =?Utf-8?B?WGF2aWVy?=, Feb 21, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    343
    =?Utf-8?B?WGF2aWVy?=
    Feb 21, 2006
  2. Neroku
    Replies:
    12
    Views:
    559
    Oliver Wong
    Feb 12, 2007
  3. Luminari
    Replies:
    23
    Views:
    806
    Wings
    Feb 11, 2007
  4. Aaron Watters

    Best way to merge/sort two sorted lists?...

    Aaron Watters, Dec 6, 2007, in forum: Python
    Replies:
    11
    Views:
    963
    Paul Rubin
    Jan 11, 2008
  5. Luminari
    Replies:
    0
    Views:
    109
    Luminari
    Feb 8, 2007
Loading...

Share This Page