comparing two arrays

Diez B. Roggisch deets at nospam.web.de
Tue Jun 20 06:09:28 EDT 2006


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

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



More information about the Python-list mailing list