I am using a python class that I'm using when filling using a for loop, and it is very slow when I'm looping through millions of data lines, and there obviously is a faster way. Perhaps I shouldn't be using a class at all, but I need to create a structure so that I can sort it.
Here is that class:
class Particle(object):
def __init__(self, ID, nH, T, metallicity,oxygen,o6,o7,o8):
self.ID = ID
self.nH = nH
self.T = T
self.metallicity = metallicity
self.oxygen = oxygen
self.o6 = o6
self.o7 = o7
self.o8 = o8
and here is how I first filled it up after reading all the individual arrays (ID, nH, T, etc.) using append, which is of course exceedingly slow:
partlist = []
for i in range(npart):
partlist.append(Particle(int(ID[i]),nH[i],T[i],metallicity[i],oxygen[i],o6[i],o7[i],o8[i]))
This takes a couple hours for 30 million values, and obviously 'append' is not the right way to do it. I thought this was an improvement:
partlist = [Particle(int(ID[i]),nH[i],T[i],metallicity[i],oxygen[i],o6[i],o7[i],o8[i]) for i in range(npart)]
but this is taking probably just as long and hasn't finished after an hour.
I'm new to python, and using indexes is not "pythonic", but I am at a loss on how to make and fill up a python object in what should only take a few minutes.
Suggestions? Thanks in advance.