Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to copy an object without referencing to it?
    primarykey
    data
    text
    <p>I'm not sure if my title is correct for what I'm looking for, but I think that the referencing is the problem.</p> <p>I have a Reader object through which I can loop:</p> <pre><code>msrun = pymzml.run.Reader(mzmlFile) for feature in msrun: print feature['id'] </code></pre> <p>With this code I get the id's, starting at 1, of all the features in msrun. However, I need to loop through the code first and get all the keys that I want and put them in a list, like this:</p> <pre><code>def getKeys(msrun, excludeList): spectrumKeys = [] done = False for spectrum in msrun: if done: break if spectrum['ms level'] == 2: for key in spectrum: if key not in excludeList and not key.startswith('MS:'): done = True spectrumKeys.append(key) spectrumKeys.extend(spectrum['precursors'][0].keys()) precursorKeys = spectrum['precursors'][0].keys() break return spectrumKeys, precursorKeys </code></pre> <p>However, if I would run this code:</p> <pre><code>msrun = pymzml.run.Reader(mzmlFile) specKeys, precursKeys = getKeys(msrun, ['title','name']) for feature in msrun: print feature['id'] </code></pre> <p>it starts of at the id that hasn't been in the loop in getKeys() (it starts at 11 instead of 1). So I guess pymzml.run.Reader() works like a generator object. So I tried copying the object. First I tried</p> <pre><code>copyMsrun = msrun specKeys, precursKeys = getKeys(copyMsrun, ['title','name']) </code></pre> <p>But this gives the same problem, if I understood correctly because doing copyMsrun = msrun makes them point to the same thing.</p> <p>Then I tried</p> <pre><code>import copy copyMsrun = copy.copy(msrun) </code></pre> <p>But I still had the same problem. I used copy.copy instead of copy.deepcopy because I don't think that the Reader objects contains other objects, and when I try deepcopy I get </p> <pre><code>TypeError: object.__new__(generator) is not safe, use generator.__new__(). </code></pre> <p>So how <strong>do</strong> I copy an object so that looping through one doesn't affect the other? Should I just do</p> <pre><code>msrun = pymzml.run.Reader(mzmlFile) copyMsrun = pymzml.run.Reader(mzmlFile) </code></pre> <p>?</p> <hr> <p>Edit: On Ade YU's comment, I tried that too but when I do </p> <pre><code>spectrumList = [] for spectrum in msrun: print spectrum['id'] spectrumList.append(spectrum) for spectrum in spectrumList: print spectrum['id'] </code></pre> <p>The first print gives me 1-10, but the second print give me ten times 10 </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