Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are mixing different concepts, I'm afraid.</p> <p>Your <code>arrtup</code> array is <strong>not</strong> an array of tuples, it's a structured <code>ndarray</code>, that is, an array of elements that look like tuples but in fact are records (<code>numpy.void</code> objects, to be exact). In your case, you defined these records to consist in 2 integers. Internally, NumPy creates your array as a 2x2 array of blocks, each block taking a given space defined by your <code>dtype</code>: here, a block consists of 2 consecutive blocks of size <code>int</code> (that is, each sub-block takes the space an <code>int</code> takes on your machine).</p> <p>When you retrieve an element with <code>arrtup[0,1]</code>, you get the corresponding block. Because this block is structured as two-subblocks, NumPy returns a <code>numpy.void</code> (the generic object representing structured blocks), which has the same <code>dtype</code> as your array.</p> <p>Because you set the size of those blocks at the creation of the array, you're no longer able to modify it. That means that you cannot transform your 2-int records into 4-int ones as you want.</p> <p>However, you can transform you structured array into an array of <em>objects</em>:</p> <pre><code>new_arr = arrtup.astype(object) </code></pre> <p>Lo and behold, your elements are no longer <code>np.void</code> but tuples, that you can modify as you want:</p> <pre><code>new_arr[0,1] = (3,4) # That's a tuple new_arr[0,1] += (4,4) # Adding another tuple to the element </code></pre> <p>Your <code>new_arr</code> is a different beast from your <code>arrtup</code>: it has the same size, true, but it's no longer a structured array, it's an array of objects, as illustrated by</p> <pre><code>&gt;&gt;&gt; new_arr.dtype dtype("object") </code></pre> <p>In practice, the memory layout is quite different between <code>arrtup</code> and <code>newarr</code>. <code>newarr</code> doesn't have the same constraints as <code>arrtup</code>, as the individual elements can have different sizes, but object arrays are not as efficient as structured arrays.</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