Help with dict and iter

mattia gervaz at gmail.com
Sun Mar 29 11:03:32 EDT 2009


Hi all, I a list of jobs and each job has to be processed in a particular 
order by a list of machines.
A simple representation is:
# Ordering of machines
JOB1 = [3, 1, 2, 4]
JOB2 = [2, 3, 1, 4]
JOBS = [JOB1, JOB2]
NJOBS = len(JOBS)
Now, I have a list of jobs and I want to have the associated list of 
machines, e.g:
[JOB1, JOB1, JOB2] --> [3, 1, 2]
My original idea was to have a dict with associated the job number and an 
iterator associated by the list of machines:
job_machine = dict((x+1, iter(JOBS[x])) for x in range(NJOBS))
Now, something like:
for x in job_list:
    print(next(job_machine[x]))
Works good, but imagine I have a list of job_list, now obviously I have a 
StopIteration exception after the
first list. So, I'm looking for a way to "reset" the next() value every 
time i complete the scan of a list.
Is it possible? Another solution can be:
empty = dict((x+1, (0, JOBS[x])) for x in range(NJOBS))
job_machine = dict((x+1, (0, JOBS[x])) for x in range(NJOBS))
and then every time do:
for job_list in p:
    for x in job_list:
        print(job_machine[x][1][job_machine[x][0]])
        job_machine.update({x:(job_machine[x][0]+1, JOBS[x-1])})
    job_machine = empty.copy()
Can you suggest me a more python way? 

Ciao,
Mattia



More information about the Python-list mailing list