Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on the representation of your "array", it looks like you're working with a <code>numpy.ndarray</code>. This becomes quite a simple problem if that is the case -- You can transform to a 1-D iterable simple by using the <code>.flat</code> attribute. To make it unique, you can just use a <code>set</code>:</p> <pre><code>set(array.flat) </code></pre> <p>This will give you a set, but you could easily get a list from it:</p> <pre><code>list(set(array.flat)) </code></pre> <p>Here's how it works:</p> <pre><code>&gt;&gt;&gt; array = np.zeros((10,12,42,53)) &gt;&gt;&gt; list(set(array.flat)) [0.0] </code></pre> <p>As a side note, there's also <code>np.unique</code> which will give you the unique elements of your array as well.</p> <pre><code>&gt;&gt;&gt; array = np.zeros((10,12),dtype=int) &gt;&gt;&gt; print array [[0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0]] &gt;&gt;&gt; np.unique(array) array([0]) &gt;&gt;&gt; array[0,5] = 1 &gt;&gt;&gt; array[4,10] = 42 &gt;&gt;&gt; np.unique(array) array([ 0, 1, 42]) </code></pre> <hr> <p>I think I finally got this one figured out:</p> <pre><code>from itertools import product items = set(tuple(a[itr+(slice(None),)]) for itr in product(*[range(x) for x in a.shape[:-1]])) print items </code></pre> <p>Seems to work. Phew!</p> <p>How this works -- the pieces that you want to keep as triplets are accessed as:</p> <pre><code>array[X,Y,:] </code></pre> <p>So, we just need to loop over all of the combinations of <code>X</code> and <code>Y</code>. That is exactly what <code>itertools.product</code> is good for. We can get the valid <code>X</code> and <code>Y</code> in an arbitrary number of dimensions:</p> <pre><code>[range(x) for x in array.shape[:-1]] </code></pre> <p>So we pass that to product:</p> <pre><code>indices_generator = product(*[range(x) for x in array.shape[:-1]]) </code></pre> <p>Now we have something that will generate the first to indices -- We just need to construct a tuple to pass to <code>__getitem__</code> that numpy will interpret as <code>(X,Y,:)</code> -- That's easy, we're already getting <code>(X,Y)</code> from indices_generator -- We just need to tack on an emtpy slice:</p> <pre><code>all_items = ( array[idx+(slice(None),)] for idx in indices_generator ) </code></pre> <p>Now we can loop over all_items looking for the unique ones with a set:</p> <pre><code>unique_items = set(tuple(item) for item in all_items) </code></pre> <p>Now turn this back into a list, or a numpy array or whatever you want for the purposes of passing it back to PHP.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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