带循环的max()函数

时间:2019-10-09 07:48:01

标签: python max

我目前正在通过Google OR-Tools学习此Job Shop问题,并且需要您的帮助来了解这一问题,

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)

我想了解这一行:

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

谢谢。

2 个答案:

答案 0 :(得分:3)

task[0] for job in jobs_data for task in job可以转移到以下

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()只需选择最大值。

答案 1 :(得分:0)

您所指的行可以转换为以下(希望更容易理解)代码:

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)