Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:34:39+00:00 2026-06-18T08:34:39+00:00

Because of array depth issues in PHP, receiving this array from Python becomes truncated

  • 0

Because of array depth issues in PHP, receiving this array from Python becomes truncated with an ellipsis (“…”). I’d like to process the array in Python before returning back to php.

Clarification: I need to maintain the inner sets [135, 121, 81]. These are R, G, B values and I’m tying to group sets that occur more than once. Values in sets need to maintain [1, 2, 3] sequence, NOT [1,2,3,4,5,6,7,8] as some answers have suggested below.

How would you simplify this 3D numpy.ndarray to a collection of unique RGB triples?

Here is how the array is printed by Python:

[[[135 121  81]
  [135 121  81]
  [135 121  81]
  ..., 
  [135 121  81]
  [135 121  81]
  [135 121  81]]

 [[135 121  81]
  [135 121  81]
  [135 121  81]
  ..., 
  [135 121  81]
  [135 121  81]
  [135 121  81]]

 [[ 67  68  29]
  [135 121  81]
  [ 67  68  29]
  ..., 
  [135 121  81]
  [135 121  81]
  [135 121  81]]

 ..., 
 [[200 170  19]
  [200 170  19]
  [200 170  19]
  ..., 
  [ 67  68  29]
  [ 67  68  29]
  [ 67  68  29]]

 [[200 170  19]
  [200 170  19]
  [200 170  19]
  ..., 
  [116 146  15]
  [116 146  15]
  [116 146  15]]

 [[200 170  19]
  [200 170  19]
  [200 170  19]
  ..., 
  [116 146  15]
  [116 146  15]
  [116 146  15]]]

Here is the code that I have attempted:

def uniquify(arr)
    keys = []

    for c in arr:
        if not c in keys:
            keys[c] = 1
        else:
            keys[c] += 1

    return keys

result = uniquify(items)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:34:39+00:00Added an answer on June 18, 2026 at 8:34 am

    Based on the representation of your “array”, it looks like you’re working with a numpy.ndarray. This becomes quite a simple problem if that is the case — You can transform to a 1-D iterable simple by using the .flat attribute. To make it unique, you can just use a set:

    set(array.flat)
    

    This will give you a set, but you could easily get a list from it:

    list(set(array.flat))
    

    Here’s how it works:

    >>> array = np.zeros((10,12,42,53))
    >>> list(set(array.flat))
    [0.0]
    

    As a side note, there’s also np.unique which will give you the unique elements of your array as well.

    >>> array = np.zeros((10,12),dtype=int)
    >>> 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]]
    >>> np.unique(array)
    array([0])
    >>> array[0,5] = 1
    >>> array[4,10] = 42
    >>> np.unique(array)
    array([ 0,  1, 42])
    

    I think I finally got this one figured out:

    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
    

    Seems to work. Phew!

    How this works — the pieces that you want to keep as triplets are accessed as:

    array[X,Y,:]
    

    So, we just need to loop over all of the combinations of X and Y. That is exactly what itertools.product is good for. We can get the valid X and Y in an arbitrary number of dimensions:

    [range(x) for x in array.shape[:-1]]
    

    So we pass that to product:

    indices_generator = product(*[range(x) for x in array.shape[:-1]])
    

    Now we have something that will generate the first to indices — We just need to construct a tuple to pass to __getitem__ that numpy will interpret as (X,Y,:) — That’s easy, we’re already getting (X,Y) from indices_generator — We just need to tack on an emtpy slice:

    all_items = ( array[idx+(slice(None),)] for idx in indices_generator )
    

    Now we can loop over all_items looking for the unique ones with a set:

    unique_items = set(tuple(item) for item in all_items)
    

    Now turn this back into a list, or a numpy array or whatever you want for the purposes of passing it back to PHP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Because data from file look like this: line 1 is name (first last), next
I would like to find the maximum depth of this array: Array ( [0]
How can I create an array of namespaces? And because it seems like a
I expect this FQL query to return a non-empty array, because there are comments
I got an $actions array, and $actions_used array. $actions_used looks like this: array(1) {
I have a bidimensional Object array in Java. Some indices aren´t used, because they
I want to create a global array, I was looking in to NSMutableArray because
I am submitting from a form to another php page which is supposed to
Due to performance issues, I have had to transfer my android opengl code from
I have a multi-dimensional multi-object array from a simplexml_import_dom() function call. A slice of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.