Mark Newbie
Mark Newbie

Reputation: 27

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.

Upvotes: 0

Views: 675

Answers (2)

alec_djinn
alec_djinn

Reputation: 10789

The line you are referring to can be translated into the following (hopefully more understandable) code:

count = []
for job in jobs_data:          #job is a list of tuples
    for task in job:           #task is one tuple
        count.append(task[0])  #task[0] is the first item of the tuple

machines_count = 1 + max(count)

Upvotes: 0

Mr_U4913
Mr_U4913

Reputation: 1344

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.

Upvotes: 3

Related Questions