Justin Justin - 25 days ago 9
Python Question

How to split dictionary values using the another key's values(integer)

I made dictionary. Like this;

output = {}
output['epoch time'] = obstimes
output['Number of satellites'] = numsat
output['OK flag'] = ok_flag
output['PRN'] = sats
output['Observation'] = result
output['Number of each data'] = a


I have to split dictionary values using the another key's values.
I show the example.

{...],
'Observation': ['22527689.086 7', None, '-578290.975 7', '-410051.96348', '22527682.94948',
'20593906.672 7', None, '-142315.266 7', '-103992.19948', '20593902.01648',
'22312367.273 6', '22312363.496 9', '-573672.801 6', '-426591.952 9', '22312363.52047',
'24649943.453 3', None, '-696710.409 3', '-499047.10745', '24649943.55545',
'21574180.359 7', None, '-440212.809 7', '-323130.81248', '21574175.09448',
'20159532.789 7', None, '35621.205 7', '26243.45049', '20159527.23449',
'24435415.719 6', '24435413.602 9', '417737.346 6', '319304.620 9', '24435413.80147',
'23108319.461 5', None, '344109.572 5', '249072.03046', '23108315.90646',
'21789481.414 7', None, '326180.505 7', '238895.05548', '21789474.12548',
'21975436.727 7', None, '277875.380 7', '205734.181 8', '21975443.121 8',
'19981973.117 9', None, '-127270.254 9', '-94211.274 9', '19981980.414 9',
'21682862.406 6', None, '242033.680 6', '179454.101 8', '21682869.020 8',
'20647552.734 7', None, '-446216.926 7', '-329944.882 9', '20647562.945 9',
'23174371.141 6', None, '-645644.090 6', '-481288.202 8', '23174379.656 8',
'21946454.047 6', None, '420271.088 6', '311362.996 8', '21946462.590 8',
'21922170.063 8', None, '-124733.763 8', '-92183.249 9', '21922177.883 9'
...],...}


I'd like to split this 'Observation' values using another list of values.

Another list of values is like this;

'Number of each data' = [[80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 75, 80, 80, 80, 65, 75, 75, 75,
70, 60, 50, 55, 55, 70, 65, 75, 75, 70, 65, 75, 75, 75, 65, 70,
70, 75, 75.....]]


I want to split Observation values using each this 'Number of each data' values.

I wish output like this;

...], 'Observation' : [80 values][80 values][80 values]...}


Please help or advice for me.

Answer

You should iterate over the 'number of each data' list:

output['ObservationMatrix'] = []
counter = 0
for value in output['Number of each data']:
    output['ObservationMatrix'].append(output['Observation][counter:counter + value])
    counter += value

And in the end, remove (if you want to) the output['Observation'] from your dictionary

Comments