comparing two arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sheldon

    comparing two arrays

    Hi,

    I have two arrays that are identical and contain 1s and zeros. Only the
    ones are valid and I need to know where both arrays have ones in the
    same position. I thought logical_and would work but this example proves
    otherwise:[color=blue][color=green][color=darkred]
    >>> a = [0,1,2,5,6,6]
    >>> b = [5,4,1,6,4,6]
    >>> Numeric.logical _and(a==6,b==6)[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> Numeric.where(a ==b,1,0)[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> Numeric.where(a ==6 and b==6,1,0)[/color][/color][/color]
    0

    The where() statement is also worhtless here. Does anyone have any
    suggestion on how to do this?

    Thanks in advance,
    Sheldon

  • Diez B. Roggisch

    #2
    Re: comparing two arrays

    Sheldon wrote:
    [color=blue]
    > Hi,
    >
    > I have two arrays that are identical and contain 1s and zeros. Only the[/color]

    Obviously they aren't identical. They may be of same size.
    [color=blue]
    > ones are valid and I need to know where both arrays have ones in the
    > same position. I thought logical_and would work but this example proves
    > otherwise:[color=green][color=darkred]
    >>>> a = [0,1,2,5,6,6]
    >>>> b = [5,4,1,6,4,6]
    >>>> Numeric.logical _and(a==6,b==6)[/color][/color]
    > 0[color=green][color=darkred]
    >>>> Numeric.where(a ==b,1,0)[/color][/color]
    > 0[color=green][color=darkred]
    >>>> Numeric.where(a ==6 and b==6,1,0)[/color][/color]
    > 0
    >
    > The where() statement is also worhtless here. Does anyone have any
    > suggestion on how to do this?[/color]


    print [i for i, _ in enumerate((None for v in zip(a, b) where v == (1,1)))]

    should give you the list of indices.

    Diez

    Comment

    • Bas

      #3
      Re: comparing two arrays

      You are comparing a normal python list to a constant, which are
      obviously unequal. Try converting your lists to arrays first
      (untested):

      import numeric/numpy as N
      a =N.array([0,1,2,5,6,6])
      b = N.array([5,4,1,6,4,6])
      print a==6 and b==6
      print N.where(a==6 and b==6)

      hth,
      Bas



      Sheldon wrote:[color=blue]
      > Hi,
      >
      > I have two arrays that are identical and contain 1s and zeros. Only the
      > ones are valid and I need to know where both arrays have ones in the
      > same position. I thought logical_and would work but this example proves
      > otherwise:[color=green][color=darkred]
      > >>> a = [0,1,2,5,6,6]
      > >>> b = [5,4,1,6,4,6]
      > >>> Numeric.logical _and(a==6,b==6)[/color][/color]
      > 0[color=green][color=darkred]
      > >>> Numeric.where(a ==b,1,0)[/color][/color]
      > 0[color=green][color=darkred]
      > >>> Numeric.where(a ==6 and b==6,1,0)[/color][/color]
      > 0
      >
      > The where() statement is also worhtless here. Does anyone have any
      > suggestion on how to do this?
      >
      > Thanks in advance,
      > Sheldon[/color]

      Comment

      • Diez B. Roggisch

        #4
        Re: comparing two arrays

        Diez B. Roggisch wrote:
        [color=blue]
        > print [i for i, _ in enumerate((None for v in zip(a, b) where v ==
        > (1,1)))]
        >
        > should give you the list of indices.[/color]

        I musunderstood your question. Use


        print [i for i, _ in enumerate((None for x, y in zip(a, b) where x == y))]

        instead.

        Diez

        Comment

        • Robert Kern

          #5
          Re: comparing two arrays

          Bas wrote:[color=blue]
          > You are comparing a normal python list to a constant, which are
          > obviously unequal. Try converting your lists to arrays first
          > (untested):
          >
          > import numeric/numpy as N
          > a =N.array([0,1,2,5,6,6])
          > b = N.array([5,4,1,6,4,6])
          > print a==6 and b==6
          > print N.where(a==6 and b==6)[/color]

          Careful there. The "and" keyword cannot be overloaded and so neither Numeric nor
          numpy does. Either N.logical_and() should be used or (since the results of a==6
          and b==6 are known to be boolean arrays) the & operator works fine as well.


          In [9]: import numpy as np

          In [10]: a = np.array([0,1,2,5,6,6])

          In [11]: b = np.array([5,4,1,6,4,6])

          In [12]: (a==6) & (b==6)
          Out[12]: array([False, False, False, False, False, True], dtype=bool)

          In [13]: np.where((a==6) & (b==6))
          Out[13]: (array([5]),)


          The OP may also find that numpy questions are best handled on numpy-discussion
          rather than comp.lang.pytho n .



          --
          Robert Kern

          "I have come to believe that the whole world is an enigma, a harmless enigma
          that is made terrible by our own mad attempt to interpret it as though it had
          an underlying truth."
          -- Umberto Eco

          Comment

          • Sheldon

            #6
            Re: comparing two arrays


            Diez B. Roggisch skrev:
            [color=blue]
            > Diez B. Roggisch wrote:
            >[color=green]
            > > print [i for i, _ in enumerate((None for v in zip(a, b) where v ==
            > > (1,1)))]
            > >
            > > should give you the list of indices.[/color]
            >
            > I musunderstood your question. Use
            >
            >
            > print [i for i, _ in enumerate((None for x, y in zip(a, b) where x == y))]
            >
            > instead.
            >
            > Diez[/color]

            Hi Diez,

            I wish I say that I understood what you wrote here but I can't.
            Do you mind explaining a little more?

            /sheldon

            Comment

            • Diez B. Roggisch

              #7
              Re: comparing two arrays

              >> print [i for i, _ in enumerate((None for x, y in zip(a, b) where x ==[color=blue][color=green]
              >> y))]
              >>
              >> instead.
              >>
              >> Diez[/color]
              >
              > Hi Diez,
              >
              > I wish I say that I understood what you wrote here but I can't.
              > Do you mind explaining a little more?[/color]

              I actually made a typo, instead of "where" in the above use "if". and the
              whole thing won't compute what you are after. This will:

              [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals]

              Sorry for that.

              Its a nested list-comprehension. Actually, the outer thingy is a
              list-comprehension while the inner is a generator expression. There is of
              course a difference, but for now this doesn't matter too much.

              a list comprehension of form

              [<expression> for <variable> in <iterable> if <condition>]

              is a more compact form for

              result = []
              for <variable> in <iterable>:
              if <condition>:
              result.append(< expression>)


              A generator expression works the same with () instead of [].

              Now let's decompose the above statment:

              (x == y for x, y in zip(a, b) ))

              <variable> = x, y
              <iterable> = zip(a, b)
              <expression> = x == y

              So: the iterable is returned by the function zip. That is a built-in which
              will take 2 or more lists and return a list of tuples, where the first
              element is from the first list, then the second and so on. So your example
              lists become:

              a = [0,1,2,5,6,6]
              b = [5,4,1,6,4,6]
              zip(a, b) = [(0,5), (1,4), (2,1), (5,6), (6,4), (6,6)]

              So iterating over this yields the pairs of (0,5) and so forth.

              Next thing is that with

              x, y = p

              python unpacks a tuple and refers to its contents by name x for the first
              one and y for the second. So overall, we loop in sync over both lists, and
              getting x and y to point to the corresponding elements.

              Now the expression x == y will result True if x == y - False otherwise.

              So the result of the inner listcomp/genexp looks like this:

              res = [False, False, False, False, False, True]

              As you can see: for each pair x,y in the original lists we come up with the
              result of comparing them.

              Now the outer listcomp using "res" instead of the genexps for clarity reads
              like this:



              [i for i, equals in enumerate(res) if equals]

              <variable> = i, equals
              <iterable> = enumerate(res)
              <expression> = i
              <condition> = equals

              enumerate is another built-in that takes an iterable and returns a tuple of

              (pos, element)

              for each element in the iterable.

              So for our list, it will return:

              [(0, False), (1, False), (2, False), (3, False), (4, False), (5, True)]

              These tupkes values are assigened to i and equals for each element of the
              above list. The condtion

              equals

              will then guarantee that only those expressions are evaluated where equals
              is True - the last pair, so to speak. The expression then only stores the
              index i. Which will give us the desired result.

              Diez

              Comment

              • Maric Michaud

                #8
                Re: comparing two arrays

                Le Mardi 20 Juin 2006 12:09, Diez B. Roggisch a écrit :[color=blue]
                > [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals][/color]

                No needs to nest comprehensions, should be :

                [ i for i, v in enumerate(zip(a , b)) if v[0] == v[1] ]


                --
                _____________

                Maric Michaud
                _____________

                Aristote - www.aristote.info
                3 place des tapis
                69004 Lyon
                Tel: +33 426 880 097

                Comment

                • Diez B. Roggisch

                  #9
                  Re: comparing two arrays

                  Maric Michaud wrote:
                  [color=blue]
                  > Le Mardi 20 Juin 2006 12:09, Diez B. Roggisch a écrit :[color=green]
                  >> [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals][/color]
                  >
                  > No needs to nest comprehensions, should be :
                  >
                  > [ i for i, v in enumerate(zip(a , b)) if v[0] == v[1] ][/color]

                  You're right, that design stemmed from my first broken version.

                  Diez

                  Comment

                  • Scott David Daniels

                    #10
                    Re: comparing two arrays

                    Diez B. Roggisch wrote:[color=blue]
                    > Maric Michaud wrote:
                    >[color=green]
                    >> Le Mardi 20 Juin 2006 12:09, Diez B. Roggisch a écrit :[color=darkred]
                    >>> [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals][/color]
                    >> No needs to nest comprehensions, should be :
                    >> [ i for i, v in enumerate(zip(a , b)) if v[0] == v[1] ][/color]
                    >
                    > You're right, that design stemmed from my first broken version.[/color]

                    Or even deconstruct to avoid the (very mildly confusing) v[0], v[1]:

                    [i for i, (left, right) in enumerate(zip(a , b)) if left == right]

                    --
                    --Scott David Daniels
                    scott.daniels@a cm.org

                    Comment

                    • Sheldon

                      #11
                      Re: comparing two arrays

                      Thanks Diez,

                      It will take a little while for this one to sink in but it gets the job
                      done now and will for future cases.

                      /Sheldon

                      Diez B. Roggisch skrev:
                      [color=blue][color=green][color=darkred]
                      > >> print [i for i, _ in enumerate((None for x, y in zip(a, b) where x ==
                      > >> y))]
                      > >>
                      > >> instead.
                      > >>
                      > >> Diez[/color]
                      > >
                      > > Hi Diez,
                      > >
                      > > I wish I say that I understood what you wrote here but I can't.
                      > > Do you mind explaining a little more?[/color]
                      >
                      > I actually made a typo, instead of "where" in the above use "if". and the
                      > whole thing won't compute what you are after. This will:
                      >
                      > [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals]
                      >
                      > Sorry for that.
                      >
                      > Its a nested list-comprehension. Actually, the outer thingy is a
                      > list-comprehension while the inner is a generator expression. There is of
                      > course a difference, but for now this doesn't matter too much.
                      >
                      > a list comprehension of form
                      >
                      > [<expression> for <variable> in <iterable> if <condition>]
                      >
                      > is a more compact form for
                      >
                      > result = []
                      > for <variable> in <iterable>:
                      > if <condition>:
                      > result.append(< expression>)
                      >
                      >
                      > A generator expression works the same with () instead of [].
                      >
                      > Now let's decompose the above statment:
                      >
                      > (x == y for x, y in zip(a, b) ))
                      >
                      > <variable> = x, y
                      > <iterable> = zip(a, b)
                      > <expression> = x == y
                      >
                      > So: the iterable is returned by the function zip. That is a built-in which
                      > will take 2 or more lists and return a list of tuples, where the first
                      > element is from the first list, then the second and so on. So your example
                      > lists become:
                      >
                      > a = [0,1,2,5,6,6]
                      > b = [5,4,1,6,4,6]
                      > zip(a, b) = [(0,5), (1,4), (2,1), (5,6), (6,4), (6,6)]
                      >
                      > So iterating over this yields the pairs of (0,5) and so forth.
                      >
                      > Next thing is that with
                      >
                      > x, y = p
                      >
                      > python unpacks a tuple and refers to its contents by name x for the first
                      > one and y for the second. So overall, we loop in sync over both lists, and
                      > getting x and y to point to the corresponding elements.
                      >
                      > Now the expression x == y will result True if x == y - False otherwise.
                      >
                      > So the result of the inner listcomp/genexp looks like this:
                      >
                      > res = [False, False, False, False, False, True]
                      >
                      > As you can see: for each pair x,y in the original lists we come up with the
                      > result of comparing them.
                      >
                      > Now the outer listcomp using "res" instead of the genexps for clarity reads
                      > like this:
                      >
                      >
                      >
                      > [i for i, equals in enumerate(res) if equals]
                      >
                      > <variable> = i, equals
                      > <iterable> = enumerate(res)
                      > <expression> = i
                      > <condition> = equals
                      >
                      > enumerate is another built-in that takes an iterable and returns a tuple of
                      >
                      > (pos, element)
                      >
                      > for each element in the iterable.
                      >
                      > So for our list, it will return:
                      >
                      > [(0, False), (1, False), (2, False), (3, False), (4, False), (5, True)]
                      >
                      > These tupkes values are assigened to i and equals for each element of the
                      > above list. The condtion
                      >
                      > equals
                      >
                      > will then guarantee that only those expressions are evaluated where equals
                      > is True - the last pair, so to speak. The expression then only stores the
                      > index i. Which will give us the desired result.
                      >
                      > Diez[/color]

                      Comment

                      • Sheldon

                        #12
                        Re: comparing two arrays

                        Thanks Diez,

                        It will take a little while for this one to sink in but it gets the job
                        done now and will for future cases.

                        /Sheldon

                        Diez B. Roggisch skrev:
                        [color=blue][color=green][color=darkred]
                        > >> print [i for i, _ in enumerate((None for x, y in zip(a, b) where x ==
                        > >> y))]
                        > >>
                        > >> instead.
                        > >>
                        > >> Diez[/color]
                        > >
                        > > Hi Diez,
                        > >
                        > > I wish I say that I understood what you wrote here but I can't.
                        > > Do you mind explaining a little more?[/color]
                        >
                        > I actually made a typo, instead of "where" in the above use "if". and the
                        > whole thing won't compute what you are after. This will:
                        >
                        > [i for i, equals in enumerate((x == y for x, y in zip(a, b))) if equals]
                        >
                        > Sorry for that.
                        >
                        > Its a nested list-comprehension. Actually, the outer thingy is a
                        > list-comprehension while the inner is a generator expression. There is of
                        > course a difference, but for now this doesn't matter too much.
                        >
                        > a list comprehension of form
                        >
                        > [<expression> for <variable> in <iterable> if <condition>]
                        >
                        > is a more compact form for
                        >
                        > result = []
                        > for <variable> in <iterable>:
                        > if <condition>:
                        > result.append(< expression>)
                        >
                        >
                        > A generator expression works the same with () instead of [].
                        >
                        > Now let's decompose the above statment:
                        >
                        > (x == y for x, y in zip(a, b) ))
                        >
                        > <variable> = x, y
                        > <iterable> = zip(a, b)
                        > <expression> = x == y
                        >
                        > So: the iterable is returned by the function zip. That is a built-in which
                        > will take 2 or more lists and return a list of tuples, where the first
                        > element is from the first list, then the second and so on. So your example
                        > lists become:
                        >
                        > a = [0,1,2,5,6,6]
                        > b = [5,4,1,6,4,6]
                        > zip(a, b) = [(0,5), (1,4), (2,1), (5,6), (6,4), (6,6)]
                        >
                        > So iterating over this yields the pairs of (0,5) and so forth.
                        >
                        > Next thing is that with
                        >
                        > x, y = p
                        >
                        > python unpacks a tuple and refers to its contents by name x for the first
                        > one and y for the second. So overall, we loop in sync over both lists, and
                        > getting x and y to point to the corresponding elements.
                        >
                        > Now the expression x == y will result True if x == y - False otherwise.
                        >
                        > So the result of the inner listcomp/genexp looks like this:
                        >
                        > res = [False, False, False, False, False, True]
                        >
                        > As you can see: for each pair x,y in the original lists we come up with the
                        > result of comparing them.
                        >
                        > Now the outer listcomp using "res" instead of the genexps for clarity reads
                        > like this:
                        >
                        >
                        >
                        > [i for i, equals in enumerate(res) if equals]
                        >
                        > <variable> = i, equals
                        > <iterable> = enumerate(res)
                        > <expression> = i
                        > <condition> = equals
                        >
                        > enumerate is another built-in that takes an iterable and returns a tuple of
                        >
                        > (pos, element)
                        >
                        > for each element in the iterable.
                        >
                        > So for our list, it will return:
                        >
                        > [(0, False), (1, False), (2, False), (3, False), (4, False), (5, True)]
                        >
                        > These tupkes values are assigened to i and equals for each element of the
                        > above list. The condtion
                        >
                        > equals
                        >
                        > will then guarantee that only those expressions are evaluated where equals
                        > is True - the last pair, so to speak. The expression then only stores the
                        > index i. Which will give us the desired result.
                        >
                        > Diez[/color]

                        Comment

                        Working...