Joining a list to make a long string
I’ve been having some performance issues with what should be an amazingly easy problem. I want to output a string with 8000 1 or 0 values space separated. I want to do this for a few thousand rows. The data is very sparse. I originally used numpy.zeros() thinking this would give me a big list of 0’s really quickly, which it does. My issue came about after I had populated the list with the appropriate 1’s, I was using along the lines of ” “.join([str(x) for x in row]). This conversion bit was terribly slow (that is, terribly slow when multiplied over thousands of rows and columns). My script was taking around 18 minutes to output all the data.
After trying some different methods I tried one of the simpler ones hoping it would work
r = “0″ * 8000
r[5] = 1
TypeError: ’str’ object does not support item assignment
Damn, next, lets try converting that string to a list, then assign. Works beautifully.
Now that we have a list of strings (we need to make sure that we’re assigning strings “1″ instead of integers now), we can just do ” “.join(r).
Total time for my script to run now? 16 seconds… don’t you hate it when it’s something so incredibly simple.
I should also put a note in here for those trying to use the python interface to FANN and running into issues with too much memory being used while training your neural network. Use the raw C interface instead, for some reason it’s so incredibly much more memory efficient.
portland construction prices test