2

I'm newbie on Python. I have this list:

a = [[0,1,2,3],[4,5,6,7,8,9], ...]
b = [[0,6,9],[1,5], ...]

a & b can have more components, depends on data. I want to know is there any intersection on these lists? If there's any intersection, I wanna have a result like this:

c = [[6,9], ...]
6
  • 2
    Intersection is an operation between two sets. You have two lists of lists, so I'm having a hard time figuring out what it is you're asking for with just the one example. Commented Apr 8, 2014 at 21:22
  • 6
    Can you elaborate on the expected outcome? c contains [6,9], but apparently not [1] which would be the intersection of a[0] and b[1]. Commented Apr 8, 2014 at 21:22
  • I'm working on spatio-temporal clustering using ST-DBSCAN. I want to modify the neighborhoods within eps1 and eps2. Eps1 for the spatial limit distance and eps2 for the temporal. I need to find the intersection of neighborhoods within eps1 and eps2. (I'm sorry, not really good in English) Commented Apr 8, 2014 at 21:32
  • Sorry, I wasn't asking for your use case (please excuse that I will not read up on the background of ST-DBSCAN) but a mathematical precise description of the function f that results in c=f(a,b) depending on the values of a and b. For example if my use case was to calculate the average height of the buildings in a city, then f would be f(x_1,x_2,...,x_n)=(x_1+x_2+...+x_n)/n. Commented Apr 8, 2014 at 22:05
  • My question is : Is there any elements in list a intersect with list b. For example, [0,1,2,3] in list a will find intersection in list b which is [0,6,9] and [1,5]. [4,5,6,7,8,9] in list a will find intersection in list b which is [0,6,9] and [1,5]. There's an intersection between [4,5,6,7,8,9] and [0,6,9], so the result will be [6,9] Commented Apr 8, 2014 at 22:15

5 Answers 5

2

The set type, built into Python, supports intersection natively. However, note that set can only hold one of each element (like a mathematical set). If you want to hold more than one of each element, try collections.Counter.

You can make sets using {} notation (like dictionaries, but without values):

>>> a = {1, 2, 3, 4, 5}
>>> b = {2, 4, 6, 8, 10}

and you can intersect them using the & operator:

>>> print a & b
set([2, 4])
Sign up to request clarification or add additional context in comments.

Comments

1

Given that intersection is an operation between two sets, and you have given two lists of lists, it's very unclear what you're looking for. Do you want the intersection of a[1] and b[0]? Do you want the intersection of every possible combination?

I'm guessing you want the intersection of every combination of two sets between your two lists, which would be:

from itertools import product
[set(x).intersection(set(y)) for x, y in product(a, b)]

1 Comment

You might want to filter out empty sets: [set(x).intersection(set(y)) for x, y in product(a, b) if len(set(x).intersection(set(y)))>0]
1

First of all, in your example code this is not a tuple, it's a list (the original question asked about lists, but references tuples in the example code).

To get an intersection of two tuples or lists, use a code like this:

set((1,2,3,4,5)).intersection(set((1,2,3,7,8)))

Comments

1

In one line:

common_set = set([e for r in a for e in r])&set([e for r in b for e in r])

Or easier:

common_set = set(sum(a,[])) & set(sum(b,[]))

Common will be a set. You can easily convert set to the list is you need it:

common_list = list(common_set)

Comments

0

Another way to do it... assuming you want the intersection of the flatten list.

>>> from itertools import chain

>>> a = [[0,1,2,3],[4,5,6,7,8,9]]
>>> b = [[0,6,9],[1,5]]

>>> list(set(chain(*a)).intersection(set(chain(*b))))
[0, 9, 5, 6, 1]

1 Comment

It's not the answer, but close with it (I guess). Can we check manually one by one? [0,6,9] with [0,1,2,3], [4,5,6,7,8,9], and so on. [1,5] with [0,1,2,3], [4,5,6,7,8,9], and so on.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.