Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want <code>out</code> to hold the values of the last list/tuple in <code>nodes</code> without them getting reset to <code>0.0</code> you need to either create a copy of that object (<code>nodes[-1]</code>) or manually copy them into a new list/tuple object, if I understand you correctly, and that's your aim. You are currently just creating a reference to nodes[-1] before you interate through the 2-dimensional array and reset all values to <code>0.0</code>, which means, <code>out</code> will still reference the referent in memory that you reset to <code>0.0</code> values.</p> <pre><code>out = nodes[-1].copy() # This is the list/tuple with the (non-reset) values that you want to return ... # iterate through the array here and reset return out </code></pre> <p>This will now return the <strong>copy</strong> (i.e. a new reference to a <em>new, unique</em> referent in memory) of the last element of <code>nodes</code> with the state it was in when you created the copy (before the reset iteration).</p> <p>Not sure if I understood you correctly but forgetting that lists and tuples <strong>are</strong> objects in Python and behave as such is an easy mistake to make.</p> <p>That means: l0 = [0, 1] l1 = l0 l2 = l0.copy() l0.append(2)</p> <pre><code>print(l0) # prints [0, 1, 2] print(l1) # prints [0, 1, 2] print(l2) # prints [0, 1] </code></pre> <p><code>l0</code> and <code>l1</code> are two references to the same object/referent in memory. Through the <code>copy()</code> operation <code>l2</code> becomes a reference to a unique and <em>non-identical</em> referent in memory.</p> <p><strong>Addition:</strong> Please note that list.copy() is, as kindly pointed out in the comments, available in Python 3 only and returns a shallow copy. Use <code>copy.copy()</code>/<code>copy.deepcopy()</code> to create a shallow/deep copy respectively; this module is available in <em>Python 2</em> and <em>Python 3</em>. Alternatively, a <em>shallow</em> copy of a list can be produced through <code>list(list_to_copy)</code>.</p> <p>Documentation here:</p> <ul> <li><a href="http://docs.python.org/2/library/copy.html" rel="nofollow">http://docs.python.org/2/library/copy.html</a></li> <li><a href="http://docs.python.org/3.3/library/copy.html" rel="nofollow">http://docs.python.org/3.3/library/copy.html</a></li> </ul>
    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