Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove elements that appear more often than once from numpy array
    primarykey
    data
    text
    <p>The question is, how can I remove elements that appear more often than once in an array completely. Below you see an approach that is very slow when it comes to bigger arrays. Any idea of doing this the numpy-way? Thanks in advance. </p> <pre><code>import numpy as np count = 0 result = [] input = np.array([[1,1], [1,1], [2,3], [4,5], [1,1]]) # array with points [x, y] # count appearance of elements with same x and y coordinate # append to result if element appears just once for i in input: for j in input: if (j[0] == i [0]) and (j[1] == i[1]): count += 1 if count == 1: result.append(i) count = 0 print np.array(result) </code></pre> <h1>UPDATE: BECAUSE OF FORMER OVERSIMPLIFICATION</h1> <p>Again to be clear: How can I remove elements appearing more than once concerning a certain attribute from an array/list ?? Here: list with elements of length 6, if first and second entry of every elements both appears more than once in the list, remove all concerning elements from list. Hope I'm not to confusing. Eumiro helped me a lot on this, but I don't manage to flatten the output list as it should be :( </p> <pre><code>import numpy as np import collections input = [[1,1,3,5,6,6],[1,1,4,4,5,6],[1,3,4,5,6,7],[3,4,6,7,7,6],[1,1,4,6,88,7],[3,3,3,3,3,3],[456,6,5,343,435,5]] # here, from input there should be removed input[0], input[1] and input[4] because # first and second entry appears more than once in the list, got it? :) d = {} for a in input: d.setdefault(tuple(a[:2]), []).append(a[2:]) outputDict = [list(k)+list(v) for k,v in d.iteritems() if len(v) == 1 ] result = [] def flatten(x): if isinstance(x, collections.Iterable): return [a for i in x for a in flatten(i)] else: return [x] # I took flatten(x) from http://stackoverflow.com/a/2158522/1132378 # And I need it, because output is a nested list :( for i in outputDict: result.append(flatten(i)) print np.array(result) </code></pre> <p>So, this works, but it's impracticable with big lists. First I got RuntimeError: maximum recursion depth exceeded in cmp and after applying sys.setrecursionlimit(10000) I got Segmentation fault how could I implement Eumiros solution for big lists > 100000 elements?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload