You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将大列表各子列表首个值设为进程ID并代入CPU调度算法

如何将大列表中每个子列表的第一个值设置为进程ID?

现有实现情况

我已经通过如下代码从.xlsx文件提取了数据:

from openpyxl import load_workbook

workbook = load_workbook(filename=r"C:\Users\sample.xlsx")
sheet = workbook.active

output = []
for row in sheet.iter_rows(min_row=2,
                           max_row=21,
                           min_col=1,
                           max_col=4,
                           values_only=True):
    listtest = []
    for cell in row:
        listtest.append(str(cell))
    output.append(listtest)

得到的输出结构如下:

表头结构:[Process ID, Arrival Time, Burst Load, Priority]
实际数据:[['1', '1', '8', '2'], ['2', '2', '9', '4'], ..., ['20', '90', '4', '14']]

现有CPU调度算法代码如下,目前使用静态赋值的参数:

def findWaitingTime(processes, n, bt, wt):
    wt[0] = 0
    # calculating waiting time
    for i in range(1, n):
        wt[i] = bt[i - 1] + wt[i - 1]

def findTurnAroundTime(processes, n, bt, wt, tat):
    # calculating turnaround
    for i in range(n):
        tat[i] = bt[i] + wt[i]

def findavgTime(processes, n, bt):
    wt = [0] * n
    tat = [0] * n
    total_wt = 0
    total_tat = 0

    # Function to find waiting time of all processes
    findWaitingTime(processes, n, bt, wt)

    # Function to find turn around time for all processes
    findTurnAroundTime(processes, n, bt, wt, tat)

    # Display processes along with all details
    print("Processes Burst time " +
          " Waiting time " +
          " Turn around time")

    # Calculate total waiting time and total turn around time
    for i in range(n):
        total_wt = total_wt + wt[i]
        total_tat = total_tat + tat[i]
        print(" " + str(i + 1) + "\t\t" +
              str(bt[i]) + "\t " +
              str(wt[i]) + "\t\t " +
              str(tat[i]))

    print("Average waiting time = " +
          str(total_wt / n))
    print("Average turn around time = " +
          str(total_tat / n))


# Driver code
if __name__ == "__main__":
    # process id's
    processes = [1, 2, 3]
    n = len(processes)

    # Burst time of all processes
    burst_time = [10, 5, 8]

    findavgTime(processes, n, burst_time)

期望输出格式如下:

Processes, Burst time, Waiting time, Turn around time
1, 1, 8, 2
2, 2, 9, 4
3, 2, 5, 5
4, 3, 5, 2
5, 6, 14, 7
Average waiting time = 8.333333333333334
Average turn around time = 16.0

我尝试通过如下方式提取子列表首个值:

listtest = []
for cell in row:
    listtest.append(str(cell))
    new_list = []
    for item in output:
        new_list.append(item[0])

output.append(listtest)

但这种方式只能将首个值提取到独立的新列表,无法和子列表中其他参数对应关联。


解决方案

核心思路

不需要单独拆分字段存到独立列表,只需要在读取Excel的时候直接把字段转成整数,然后从读取结果里按索引提取对应字段传给调度算法即可,同时调整打印逻辑匹配需求格式即可。

完整修改后代码

from openpyxl import load_workbook

def findWaitingTime(processes, n, bt, wt):
    wt[0] = 0
    for i in range(1, n):
        wt[i] = bt[i - 1] + wt[i - 1]

def findTurnAroundTime(processes, n, bt, wt, tat):
    for i in range(n):
        tat[i] = bt[i] + wt[i]

def findavgTime(processes, n, bt):
    wt = [0] * n
    tat = [0] * n
    total_wt = 0
    total_tat = 0

    findWaitingTime(processes, n, bt, wt)
    findTurnAroundTime(processes, n, bt, wt, tat)

    # 调整打印表头匹配需求格式
    print("Processes, Burst time, Waiting time, Turn around time")

    for i in range(n):
        total_wt = total_wt + wt[i]
        total_tat = total_tat + tat[i]
        # 调整打印格式,用逗号分隔
        print(f"{processes[i]}, {bt[i]}, {wt[i]}, {tat[i]}")

    print(f"Average waiting time = {total_wt / n}")
    print(f"Average turn around time = {total_tat / n}")


if __name__ == "__main__":
    # 1. 读取Excel数据,直接转成整数格式
    workbook = load_workbook(filename=r"C:\Users\sample.xlsx")
    sheet = workbook.active
    process_data = []
    for row in sheet.iter_rows(min_row=2,
                               max_row=21,
                               min_col=1,
                               max_col=4,
                               values_only=True):
        # 子列表顺序:[进程ID, 到达时间,  Burst时间, 优先级],全部转整数
        process_item = [int(cell) for cell in row]
        process_data.append(process_item)
    
    # 2. 提取对应字段传给调度算法
    processes = [item[0] for item in process_data]  # 提取所有进程ID
    burst_time = [item[2] for item in process_data] # 提取所有Burst时间(第三个字段,索引为2)
    n = len(processes)

    # 3. 调用调度计算
    findavgTime(processes, n, burst_time)

说明

  • 读取Excel的时候直接把单元格值转成整数,避免后续类型转换的问题
  • 提取字段的时候直接按索引从process_data的每个子元素里取,天然保持对应关系,不会出现参数不匹配的问题
  • 调整了打印的格式,输出和要求的逗号分隔格式完全一致

内容的提问来源于stack exchange,提问作者MadVags

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 06:12:02