Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is because the <code>stars</code> list is mutable - it is a changeable variable. When you call <code>stars.append</code>, it does not create a new list - it simple edits the existing list. And when you call <code>history.append(x)</code>, python does not create a fresh, clean copy of <code>x</code> - it assumes you want the "real" <code>x</code> to be placed in the <code>history</code> array.</p> <p>If you'd like to copy the state of the list at each iteration, there are several options (see <a href="https://stackoverflow.com/questions/2612802/how-to-clone-a-list-in-python">How to clone a list in python?</a>). </p> <p>The <a href="http://docs.python.org/2/library/copy.html" rel="nofollow noreferrer"><code>copy</code> module</a> will do the trick. It offers both 'shallow' and 'deep' copies - roughly speaking, deep copies attempt to also copy any other variables they find inside an object (for example, lists containing other lists), while shallow ones just go one layer down:</p> <p>Here's shallow copies with <code>copy</code>:</p> <pre><code>import copy history=[] for n in range(100): history.append(copy.copy(gal.time_stepper(.1))) </code></pre> <p>And deep copies:</p> <pre><code>import copy history=[] for n in range(100): history.append(copy.deepcopy(gal.time_stepper(.1))) </code></pre> <p>Slicing the array will work, since it always shallow copies as an intermediate step:</p> <pre><code>history = [] for n in range(100): history.append(gal.time_stepper(.1)[:]) </code></pre> <p>You can also call <code>list</code> on the existing list - this typecast-esque operation always returns a new list object (again, shallowly):</p> <pre><code>history = [] for n in range(100): history.append(list(gal.time_stepper(.1))) </code></pre>
    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. 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