Python 将以前方法调用中的变量追加到以后的方法调用中

Python 将以前方法调用中的变量追加到以后的方法调用中,python,python-2.7,networkx,Python,Python 2.7,Networkx,您好,下面还显示了运行此代码的示例输出,脚本也在运行,您可以运行它来了解它的功能。我的问题是,给出了对route()函数的三个不同调用,我可以添加哪些代码来获得所有先前目标函数值的列表,即第79行的obj变量 desired output = obj_rslt = [20.989285714285714, 21.166176470588233, 25.8656 ] 我试图使用copy.copy()但它不起作用,我需要一个上面显示的所有值的列表,以便在另一个函数中进一步工作。多谢各位 #imp

您好,下面还显示了运行此代码的示例输出,脚本也在运行,您可以运行它来了解它的功能。我的问题是,给出了对route()函数的三个不同调用,我可以添加哪些代码来获得所有先前目标函数值的列表,即第79行的obj变量

 desired output = obj_rslt = [20.989285714285714, 21.166176470588233, 25.8656 ]
我试图使用copy.copy()但它不起作用,我需要一个上面显示的所有值的列表,以便在另一个函数中进一步工作。多谢各位

#import statements
import copy
import networkx as nx
import random as rand

#Define nodes and Edges
pos = {1001:(-42503,-3748871),1002:(-42267,-3749806),1003:(-40938,-3750235),1004: (-39452,-3750624),1005:(-39985,-3749564),1006:(-38473,-3749615),1007:(-41714,-3747171),1008:(-42279,-3745275),1009:(-41853,-3744185),1010:(-42000,-3746561),1011:(-42651,-3746188),1012:(-42195,-3747788),1013:(-41498,-3748890),1014:(-40366,-3748684),1015:(-43036,-3750284)}

edge = [(1001, 1003,{'length':0.35}),(1001, 1004,{'length':0.46}),(1001, 1009,{'length':0.49}),(1002, 1007,{'length':0.22}),(1002, 9972,{'length':0.54}),(1002, 1013,{'length':0.59}),(1003, 1014,{'length':0.25}),(1004, 1010,{'length':0.29}),(1004, 1013,{'length':0.57}),(1004, 1003,{'length':0.43}),(1004, 1006,{'length':0.37}),(1005, 1002,{'length':0.23}),(1005, 14566,{'length':0.72}),(1006, 1005,{'length':0.6}),(1007, 1003,{'length':0.39}),(1007, 1010,{'length':0.11}),(1009, 1001,{'length':0.51}),(1010, 1005,{'length':0.2}),(1011, 1004,{'length':0.37}),(1012, 1006,{'length':0.17}),(1013, 1005,{'length':0.19}),(1013, 1007,{'length':0.21}),(1014, 1005,{'length':0.35}),(1014, 1009,{'length':0.51})]

#Create the graph and add the nodes and edges
X = nx.MultiDiGraph()
X.add_nodes_from(pos.keys())
X.add_edges_from(edge)

def routes():

    """ This function cretaes busroutes """

    individual = []
    shortest_path_length = []
    num_routes = 3

    #Generate the bus routes
    for i in xrange(num_routes):

            while True:

                try:

                    A = int(rand.choice(pos.keys()))
                    B = int(rand.choice(pos.keys()))
                    path = nx.dijkstra_path(X,A,B,weight='length')
                    individual.append(path)
                    pathlength = round(nx.dijkstra_path_length(X,A,B),2)

                    if pathlength > 1:
                        shortest_path_length.append(pathlength)
                        break

                    else: pathlength

                except:pass

    # Loop through the list of shortest path nodes to get the nodes for the bus route
    #bus_route_nodes = [map(lambda x: str(x) + '.0', l) for l in individual]

    veh_spid = []
    veh_hdw = []
    obj_rslt = []
    for ind in individual:

        try:
            headway = rand.randint(2, 30)
            veh_hdw.append(headway)
            speed = rand.choice([2,15,66])
            veh_spid.append(speed)

        except: pass

    # Print bus route features
    print 'OUTPUTS:'
    print 'Route Name:', str(ind[0]) + ' ' + '-' + ' ' + str(ind[-1])
    print 'Route Number:', individual.index(ind) + 1
    print 'Route headway = ',headway
    print 'Route speed = ',speed
    print 'shortest path length', shortest_path_length

    # Average network characteristics gotten by taken the mean of individual characteristics that make up each network
    ntwk_len = sum(shortest_path_length)
    ntwk_spid =  sum(veh_spid)/len(veh_spid)
    ntwk_hdwy = sum(veh_hdw )/len(veh_hdw)

    #Calculate objective function values
    obj = [0]
    obj = copy.copy(obj)
    obj = ( (ntwk_len/ntwk_spid) + 5 * (60/ntwk_hdwy) + (ntwk_len) )
    obj_rslt.append(obj)

    print 'obj_rslt', obj_rslt
    print

    return individual

#Three distinct method calls
routes()
routes()
routes()

OUTPUTS:
Route Name: 1014 - 1001
Route Number: 6
Route headway =  29
Route speed =  2
shortest path length [1.8, 2.77, 1.02]
obj_rslt [20.989285714285714]

OUTPUTS:
Route Name: 1003 - 1007
Route Number: 9
Route headway =  5
Route speed =  66
shortest path length [2.37, 2.57, 1.05]
obj_rslt [21.166176470588233]

OUTPUTS:
Route Name: 1012 - 1013
Route Number: 6
Route headway =  6
Route speed =  66
shortest path length [2.2, 1.85, 1.59]
obj_rslt [25.8656]

desired output = obj_rslt = [20.989285714285714, 21.166176470588233, 25.8656 ]

问题是您总是将
obj\u rslt
设置为
[]
在定义了
pos
edge
之后,将赋值
obj\u rslt=[]
移出方法,这样您就没事了

#...your code
obj_results = []
def routes():
    #do some computation and assume you store it in
    #a variable called res
    obj_results.append(res)
    return res

routes()
routes()
routes()

print obj_results

输出将是一个包含所有三个结果的列表

您是否想过将所有这一切都放在一个类中,并创建一个跟踪结果的实例变量?除了此方法被另一个只能将其用作函数的代码调用之外,我会这样做。这三个独立的调用实际上模拟了在我提到的函数中调用它的方式,但我建议使用基于类的方法=)非常感谢,这非常有用。请投赞成票。当做