[英]How to print a nested list of objects with the highest sum in python
I'm still learning python and i'm still having a tough time working with objects, i'm trying to write a program that calculates the critical path of a set of activities in a project, I've been able to get the critical path of the activities and they're stored as a list of nested objects with each object having it's different properties like id, predecessor and duration, the problem i'm having is to print out the result properly, i want to print out the path that has the longest duration and the path itself that gives that value 我仍在学习python,并且在处理对象方面仍然遇到困难,我正在尝试编写一个程序来计算项目中一组活动的关键路径,我已经能够获得关键活动的路径,它们存储为嵌套对象的列表,每个对象具有其不同的属性,例如id,前任和持续时间,我遇到的问题是正确打印出结果,我想打印出路径持续时间最长的路径以及提供该值的路径本身
class criticalPath:
def __init__(self):
'''
Initialize all the variables we're going to use to calculate the critical path
'''
self.id = None
self.pred = tuple()
self.dur = None
self.est = None
self.lst = None
#list to store all the objects
self.all_objects = list()
def set_properties(self, name, predecessor, duration):
self.id = name
self.pred = tuple(predecessor)
self.dur = duration
def main():
#starting_nodes = list()
object_list = list()
A = criticalPath()
A.set_properties('A', '0', 3)
B = criticalPath()
B.set_properties('B', '0', 6)
C = criticalPath()
C.set_properties('C', 'A', 1)
D = criticalPath()
D.set_properties('D', 'B', 4)
tmp_list = list()
tmp_list.append(A)
tmp_list.append(C)
object_list.append(tmp_list)
tmp_list = list()
tmp_list.append(B)
tmp_list.append(D)
object_list.append(tmp_list)
print('The total duration of the project is {}'.format(max([sum([node.dur for node in object]) for object in object_list])))
#print(max(object_list.id, key=sum(object_list.dur)))
if __name__ == '__main__': main()
I've been able to print out the total duration which in this case is 10, the last line i commented out is my last attempt at comparing the objects id in object_lists based on their individual duration which is the property 'id' and 'dur' of each object, so basically i want to get an output like this 我已经能够打印出总持续时间,在这种情况下为10,我注释掉的最后一行是我最后一次尝试根据对象的单个持续时间(即属性“ id”和“ dur”)比较object_lists中的对象id ',所以基本上我想得到这样的输出
The critical path of the project is B==>D and the total duration of the project is 10 项目的关键路径为B ==> D,项目的总工期为10
class criticalPath:
def __init__(self):
'''
Initialize all the variables we're going to use to calculate the critical path
'''
self.id = None
self.pred = tuple()
self.dur = None
self.est = None
self.lst = None
#list to store all the objects
self.all_objects = list()
def set_properties(self, name, predecessor, duration):
self.id = name
self.pred = tuple(predecessor)
self.dur = duration
def main():
#starting_nodes = list()
object_list = list()
A = criticalPath()
A.set_properties('A', '0', 3)
B = criticalPath()
B.set_properties('B', '0', 6)
C = criticalPath()
C.set_properties('C', 'A', 1)
D = criticalPath()
D.set_properties('D', 'B', 4)
tmp_list = list()
tmp_list.append(A)
tmp_list.append(C)
object_list.append(tmp_list)
tmp_list = list()
tmp_list.append(B)
tmp_list.append(D)
object_list.append(tmp_list)
print(object_list)
print('The total duration of the project is {}'.format(max([sum([node.dur for node in object]) for object in object_list])))
print([path.id for path in max(object_list, key=lambda ls: sum(obj.dur for obj in ls))])
if __name__ == '__main__': main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.