gpt4 book ai didi

python - 基于 Id 和依赖关系对工作订单进行排序的算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:36:35 25 4
gpt4 key购买 nike

<分区>

我有一个订单列表:

id, dependency_id
1, 2
1, 3
3, 4
5, 2
3, 6

我必须输出这样的东西:

Id: 1, Name: Order 1
Dependencies
Id: 2, Name: Order 2
Id: 3, Name: Order 3
Dependencies
Id: 4, Name: Order 4
Id: 6, Name: Order 6
Id: 5, Name: Order 5
Dependencies
Id: 2, Name: Order 2

我应该使用哪种递归算法?我卡住了

这是我目前所拥有的:

dependencies = {}
def read():
f = open("dependencies.txt", "r")
# contents = f.read() #reads the whole file
#print(contents) #prints everything
#print(contents[0]) #prints first letter
lines = f.readlines()
for i in range(1, len(lines)):
dep = lines[i].split(",")
dep[1] = dep[1].rstrip()
try:
depList = dependencies.get(dep[0], [])
depList.append(dep[1])
dependencies[dep[0]] = depList
except:
dependencies[dep[0]] = [dep[1]]
if __name__ == "__main__":
read()

返回:{'1': ['2', '3'], '3': ['4', '6'], '5': ['2']}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com