【问题标题】:max() function with for loops带有 for 循环的 max() 函数
【发布时间】:2019-10-09 07:48:01
【问题描述】:

我目前正在从 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)

谢谢。

【问题讨论】:

标签: python max


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您所指的那行代码可以翻译成以下(希望更容易理解)代码:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 2012-12-21
      相关资源
      最近更新 更多