简体   繁体   中英

Find unique elements within a certain range of coordinates

I'm trying to determine all unique elements within a list based on the xy-coordinates. The list looks like the following structure:

List =[[[Picture1, [X-Coordinate, Y-Coordinate]], [Picture1, [X-Coordinate, Y-Coordinate]]],
      [[Picture2, [X-Coordinate, Y-Coordinate]], [Picture2, [X-Coordinate, Y-Coordinate]]],
      ....]

This is the actuall list:

MyList = [[['IMG_6094.jpg', [2773.0, 240.0]], ['IMG_6094.jpg', [2773.0, 240.0]]],
         [['IMG_6096.jpg', [1464.0, 444.0]], ['IMG_6096.jpg', [3043.0, 2358.0]]],
         [['IMG_6088.jpg', [1115.5, 371.5]]],
         [['IMG_6090.jpg', [3083.0, 1982.5]], ['IMG_6090.jpg', [3083.0, 1982.5]]],
         [['IMG_6093.jpg', [477.0, 481.0]], ['IMG_6093.jpg', [450.0, 487.5]]]]  

As you can see, there are sometimes elements that have the same coordinates within a picture or are at least very close to each other. What I need to do is to throw out all non-unique or very close elements based on one of the coordinates (doesn't matter if its x or y). The list should look like this:

MyList = [[['IMG_6094.jpg', [2773.0, 240.0]], --- thrown out because copy of first element ---],
         [['IMG_6096.jpg', [1464.0, 444.0]], ['IMG_6096.jpg', [3043.0, 2358.0]]],
         [['IMG_6088.jpg', [1115.5, 371.5]]],
         [['IMG_6090.jpg', [3083.0, 1982.5]], --- thrown out because copy of first element---],
         [['IMG_6093.jpg', [477.0, 481.0]], --- thrown out because e.g. abs(x-coordinates) < 30]  

Could someone provide a elegant solution?

Thanks in advance!

you create for img a list of innteger that are close to it, than for the other points on that img, you check if their x or y or in the list, and if so add them to the remove list btw when removing using the index make sure to start with largest x,y so you don't change the lower point index before removing them

 remove = []
 close = 30
 for i in range(len(MyList)):
     xList = []
     yList = []
     for j in range(len(MyList[i])):
         if int(MyList[i][j][1][0]) in xList or int(MyList[i][j][1][1]) in yList:
             remove.append([i,j])
             continue
         x = int(MyList[i][j][1][0])
         y = int(MyList[i][j][1][1])
         xList+= list(range(x-close,x+close))
         yList+= list(range(y-close,y+close))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM