I was processing some data that I got while doing a project and coded this:
# Parse data from txt file
data = numpy.loadtxt(inFileName + '.txt', skiprows=3)
freq = data[:, 0] # Mirror Frequency (Hz)
sdfreq = data[:, 1] # σ Frequency (Hz)
dist = data[:, 2] # ∆X (m)
sddist = data[:, 3] # σ ∆X (m)
And I realized that the last 4 lines look repetitive and I was obviously not going to repeat doing that if I had 1000 more data parameters. I could obviously parse it into a dictionary but that would force me to call dataset['freq'][0] instead of simply freq[0]. I could also parse it into an object and call dataset.freq[0], which would be better. But, is there a way I can compact the code and still have the ability to use freq[0] (without using exec())?
freq, sdfreq, dist, sddist = data. I'm not sure what the numpy equivalent is.freq, sdfreq, dist, sddist = data[:]dtypeandnamesis possible to get a structured array, for which you could access fields by name, e.g.data['freq']etc.