Adding object instances to a list only works with tedious depth, how can I change this?
I have a pymzml.run.Reader class from the pymzml package. This is a generator object, and when passed through it yields instances of the Spectrum class (also from the pymzml package). I compare different instances to each other. Since pymzml.run.Reader is a generator object, after passing through them, they can no longer be used, so I save them in a comparison list later.
However, when I save them in the list and then go through the list that prints out the id of the spectra, it shows that it only stores the last spectrum. To clarify:
import pymzml def test(msrun): for spectrum in msrun: print spectrum['id'] spectrumList.append(spectrum) print '-'*20 for i in spectrumList: print i['id'] msrun = pymzml.run.Reader(r'JG_Ti02-C1-1_C2-01A_file1.aligned.mzML')
1 2 3 4 5 -------------------- 5 5 5 5 5
The pymzml has a deRef () function that makes a deep copy of the spectrum, so the following works correctly:
import pymzml def test(msrun): for spectrum in msrun: print spectrum['id'] spectrumList.append(spectrum.deRef()) msrun = pymzml.run.Reader(r'JG_Ti02-C1-1_C2-01A_file1.aligned.mzML')
However, creating deepcopies is the main bottleneck I am trying to get out of the application. How do I add instances of the spectrum to the list so that not only the last spectrum is added several times?
source to share
You can't just keep the last spectrum - you are doing everything right to keep every object in the list.
The problem is that you get the same object over and over.
in a loop to get its memory address will show that it is one object that is repeated with its
and other attributes.
Although you don't have to
, you need to make a copy. Try it
and look at the source
to see how it copies it.
Chances are, you need
to make each one to make them independent - otherwise, why did the class provide a custom method?
source to share