0

We are given the following two lists of random numers:

import numpy as np
import random 

random.seed(1)
first  = random.sample(xrange(10), 5)

random.seed(2)
second = random.sample(xrange(10), 5)

print("first ="), first
print("second ="), second
-------------------------

first = [1, 7, 6, 9, 2]
second = [9, 8, 0, 7, 5]

of which we want to find the intersection using a line-by-line standard comparison as

loop = []

for first_element in first:
   for second_element in second:
       if first_element ==  second_element:
          loop.append(first_element)

print loop 
----------
[7, 9]

I wanted to make that more Pythonic with a comprehensive assignment upon conditions as

comprehension = [[first_element for second_element in second if
                first_element == second_element] for first_element 
                in first]

print comprehension
-------------------
[[], [7], [], [9], []]

The two results are different (the last also show the un-matched results), therefore I guess there is a difference in the two assignments. The former (using a standard for...for...if) is exactly what I want to have, yet I would like the latter approach since I have got many more of them and I want to avoid writing inner loops all the times.

gented
  • 1,620
  • 1
  • 16
  • 20

2 Answers2

2

The second result is different because you've defined another list that is occasionally empty.

comprehension = [
    [first_element for second_element in second
     if first_element == second_element]  # a list with some number
                                          # of first_elements in it
     for first_element in first]  # the outer list

Instead you should just use a flat list comprehension and tack the filter on the end.

result = [first_el for first_el in first 
          for second_el in second if first_el == second_el]

This might be more simply

result = [first_el for first_el in first if first_el in second]

Except for the corner case where first_el appears more than once in second. If this corner case can be ignored, then the best way to do this is:

result = set(first).intersection(second)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • Indeed, that is exactly the reason. The flat list comprehension can be adapted along the same lines to more complicated conditions and would still work. – gented Oct 10 '15 at 23:40
1

For calculating intersections, unions, etc., sets are the best option:

>>> list(set(first) & set(second))
[9, 7]
MattDMo
  • 100,794
  • 21
  • 241
  • 231