Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - Splitting an array into two using an optimized for loop
    primarykey
    data
    text
    <p>This is a followup question to a question I posted <a href="https://stackoverflow.com/questions/20223962/python-operations-on-all-elements-of-an-array-without-a-loop">here</a>, but it's a very different question, so I thought I would post it separately.</p> <p>I have a Python script which reads an very large array, and I needed to optimize an operation on each element (see referenced SO question). I now need to split the output array into two separate arrays.</p> <p>I have the code:</p> <pre><code>output = [True if (len(element_in_array) % 2) else False for element_in_array in master_list] </code></pre> <p>which outputs an array of length <code>len(master_list)</code> consisting of True or False, depending on if the length of <code>element_in_array</code> is odd or even. My problem is that I need to split <code>master_list</code> into two arrays: one array containing the <code>element_in_array</code>'s that correspond to the <code>True</code> elements in <code>output</code> and another containing the <code>element_in_array</code>'s corresponding to the <code>False</code> elements in <code>output</code>.</p> <p>This can clearly be done with traditional array operators such as <code>append</code>, but I need this to be as optimized and as fast as possible. I have many millions of elements in my <code>master_list</code>, so is there a way to accomplish this without directly looping through <code>master_list</code> and using <code>append</code> to create two new arrays.</p> <p>Any advice would be greatly appreciated. Thanks!</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