YURI
YURI

Reputation: 11

How to find out duplicate in two lists of coordinates?

I have two lists of coordinates, which is the start point and end point of routes. I need to find out which route connect to which route. i.e. start_point of a route == end_point of another route. So I need to figure out the duplicated coordinates in start_point and end_point.

For example,

start_point = [[1,2],[2,4],[3,5]]
end_point = [[3,3],[3,4],[3,5]]

I expect to get [3,5] as a return. Is there any build in function of python or package can accomplish this goal? or do I need to write a recursive loop for this?

Upvotes: 1

Views: 176

Answers (3)

Vinay Chaudhari
Vinay Chaudhari

Reputation: 128

# Method 1 : Using for loop
for item in end_point:
  if item in start_point:
    print(item)

# Method 2 : Using List Compression
print([sublist for sublist in end_point if sublist in start_point])

Output:

[3, 5]
[[3, 5]]

Upvotes: 0

Aniket Kumar
Aniket Kumar

Reputation: 324

I guess you are trying to get the intersection of two lists. Here is an approach I would prefer

res= set(map(tuple, start_point )) & set(map(tuple, end_point ))
res_list = list(map(list, res))

To get intersection, I am converting the list into set and performing AND operation on it. Then converting it back to list.

For more details you can check this page

Upvotes: 0

mozway
mozway

Reputation: 260470

You can use zip to iterate in parallel, and a list comprehension:

start_point = [[1,2],[2,4],[3,5]]
end_point = [[3,3],[3,4],[3,5]]

out = [s for s,e in zip(start_point, end_point) if s==e]

output:

[[3, 5]]

Upvotes: 2

Related Questions