How to print a nested list of objects with the highest sum in python -
i'm still learning python , i'm still having tough time working objects, i'm trying write program calculates critical path of set of activities in project, i've been able critical path of activities , they're stored list of nested objects each object having it's different properties id, predecessor , duration, problem i'm having print out result properly, want print out path has longest duration , path gives value
class criticalpath: def __init__(self): ''' initialize variables we're going use calculate critical path ''' self.id = none self.pred = tuple() self.dur = none self.est = none self.lst = none #list store 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() = 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 project {}'.format(max([sum([node.dur node in object]) object in object_list]))) #print(max(object_list.id, key=sum(object_list.dur))) if __name__ == '__main__': main()
i've been able print out total duration in case 10, last line commented out last attempt @ comparing objects id in object_lists based on individual duration property 'id' , 'dur' of each object, want output this
the critical path of project b==>d , total duration of project 10
class criticalpath: def __init__(self): ''' initialize variables we're going use calculate critical path ''' self.id = none self.pred = tuple() self.dur = none self.est = none self.lst = none #list store 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() = 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 project {}'.format(max([sum([node.dur node in object]) object in object_list]))) print([path.id path in max(object_list, key=lambda ls: sum(obj.dur obj in ls))]) if __name__ == '__main__': main()
Comments
Post a Comment