Reputation: 957
My data is generated in not quite a useful manner with first a few spaces, then the index number (1-12 in this example) and then the actual value associated with the index. What I would like is to split the string in two lists: 1 list with the indices and 1 list with the values. I've written the following code that works for what I want. However, it seems cumbersome and takes many seconds to run for a data set of a few thousand rows. Is there a way to speed this up for large data sets?
data = [' 11.814772E3',
' 2-1.06152E3',
' 33.876477E1',
' 4-2.65704E3',
' 51.141537E4',
' 61.378482E4',
' 71.401565E4',
' 86.782599E3',
' 9-1.22921E3',
' 103.400054E3',
' 111.558086E3',
' 121.017818E4']
values_total = [] #without empty strings
location = [] #index when id goes to value
ids = [] #Store ids
values = [] #Store values
step_array = np.linspace(1,1E3,1E3) #needed to calculate index values
for i in range(len(data)):
#Check how many indices have to be removed
location.append([])
location[i].append(int(math.log10(step_array[i]))+1)
#Store values after empty strings
for j in range(len(data[i])):
values_total.append([])
if data[i][j] != ' ':
values_total[i].append(data[i][j])
#Split list based on calculated lengths
ids.append(values_total[i][:location[i][0]])
values.append(values_total[i][location[i][0]:])
Upvotes: 0
Views: 49
Reputation: 3285
You could try with the code below:
indices = []
vals = []
for i, d in enumerate(data, 1): # enumerate starting from 1, so we know current index
tmp = d.strip() # remove whitespace
split_idx = len(str(i)) # figure out the length of the current index
indices.append(i) # current index
vals.append(float(tmp[split_idx:])) # everything after current index length
Upvotes: 1