python - List of all arrays not contained in other lists of arrays -


i have list of 2 dimensional points, represented 2 element long lists/arrays. e.g.:

points =        [[ 10.       ,  10.       ],        [ 11.       ,  10.       ],        [ 10.5      ,   9.1339746],        [ 10.5      ,  10.       ],        [ 10.75     ,   9.5669873],        [ 10.25     ,   9.5669873],        [  2.       ,   2.       ],        [  3.       ,   2.       ],        [  2.5      ,   1.1339746],        [  2.5      ,   2.       ],        [  2.75     ,   1.5669873],        [  2.25     ,   1.5669873]] 

i want have list not contain elements of first list.

exclude = [[2., 2.], [3., 2.], [2.5, 2.]] 

unfortunately

new_list = [p p in points if p not in exclude] 

will produce

[[ 10.       ,  10.       ],  [ 11.       ,  10.       ],  [ 10.5      ,   9.1339746],  [ 10.5      ,  10.       ],  [ 10.75     ,   9.5669873],  [ 10.25     ,   9.5669873],  [  2.75     ,   1.5669873],  [  2.25     ,   1.5669873]] 

instead of

[[ 10.       ,  10.       ],  [ 11.       ,  10.       ],  [ 10.5      ,   9.1339746],  [ 10.5      ,  10.       ],  [ 10.75     ,   9.5669873],  [ 10.25     ,   9.5669873],  [  2.5      ,   1.1339746],  [  2.75     ,   1.5669873],  [  2.25     ,   1.5669873]] 

it seems python removes elements here have @ least 1 element in common (and not in common :/ ).

is there nice/short/elegant way exclude elements if not contained in first list?

note: since question has been tagged numpy, i'm assuming points numpy array. if that's true, generate boolean mask (array) using np.logical_and , np.logical_or:

import numpy np  points = np.array(       [[ 10.       ,  10.       ],        [ 11.       ,  10.       ],        [ 10.5      ,   9.1339746],        [ 10.5      ,  10.       ],        [ 10.75     ,   9.5669873],        [ 10.25     ,   9.5669873],        [  2.       ,   2.       ],        [  3.       ,   2.       ],        [  2.5      ,   1.1339746],        [  2.5      ,   2.       ],        [  2.75     ,   1.5669873],        [  2.25     ,   1.5669873]])  exclude = [[2., 2.], [3., 2.], [2.5, 2.]]  mask = np.logical_or.reduce(     [np.logical_and.reduce(         [points[:,idx] == ex[idx] idx in range(len(ex))]) ex in exclude])  new_points = points[~mask] print(new_points) 

prints

[[ 10.         10.       ]  [ 11.         10.       ]  [ 10.5         9.1339746]  [ 10.5        10.       ]  [ 10.75        9.5669873]  [ 10.25        9.5669873]  [  2.5         1.1339746]  [  2.75        1.5669873]  [  2.25        1.5669873]] 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -