Search code examples
pythonmaxor-tools

max() function with for loops


I am currently studying this Job Shop Problem from Google OR-Tools and I need your help to understand this one,

jobs_data = [  # task = (machine_id, processing_time).
        [(0, 3), (1, 2), (2, 2)],  # Job0
        [(0, 2), (2, 1), (1, 4)],  # Job1
        [(1, 4), (2, 3)]  # Job2
    ]

    machines_count = 1 + max(task[0] for job in jobs_data for task in job)
    all_machines = range(machines_count)

I want to understand this line:

machines_count = 1 + max(task[0] for job in jobs_data for task in job)

Thanks.


Solution

  • task[0] for job in jobs_data for task in job can be transferred into the following

    new_list = []
    
    for job in jobs_data: # for each Job
        for task in job: # for each task 
            print(task[0]) 
            new_list.append(task[0]) # get the id
    

    max() just select the maximum.