Reputation: 387
I would like to perform the following: Given a total number of observations (in this case the variable 'total_models'), I would like to parse this for parallel processing by a given number of python sessions ('sessions' variable and 'by' variable). I figure it best to perform this task using a dictionary.
The desired results should can be found in the 'obs_dict' object. For any given input to 'total_models', 'sessions' and 'by'. Can you assist in creating the desired output in a dictionary object? If possible, I would like to see the answer using some sort of list or dictionary comprehension.
total_models=1000000
sessions=4
by=int(total_models/sessions)
### Desired Output.
obs_dict={1:'0:250000',2:'250001:500000',3:'500001:750000',4:'750001:1000000'}
Upvotes: 2
Views: 72
Reputation: 722
obs = {i+1: str(i*by+1)+':'+str((i+1)*by) for i in range(sessions)}
For the odd models it would seem wrapping a ceil the division will ensure we we do not go over the 'total_models'
total_models=1000326
sessions=5
by=math.ceil(total_models/sessions)
obs = {i+1: str(i*by+1)+':'+str(min((i+1)*by,total_models)) for i in range(sessions)}
Upvotes: 5