1

我一直在尝试在 Python 中使用列表。我调用它一次以在一个循环中存储一个值。然后我在另一个循环中再次调用它,但是当我向列表中添加一些内容时,它已经用新条目覆盖了我的旧条目,然后我才真正添加了新条目。可能是我对 Python 不是很了解,但我会在下面贴一个简单版本的代码,然后是完整版本:

ret=[]
plan=argument
for i in range(x):
    plan.changeY
    ret.append(plan)
    plan=argument
for i in range(Z):
    plan.changeD
    ret.append(plan)
    plan=argument

在我进行第二个附加之前出现了问题:第一个附加的所有值都被更改了。它的代码如下。

global PLANCOUNTER
global VARNUM
ret=[]
ret=[]
plan = node.state
keep = plan
if printPlans:
    print "fringe[0].state:",plan.id, "---"
    printstate(plan)
if node.parent:
    print "parent: plan",node.parent.state.id
if len(plan.openconds)>0:
    print plan.openconds[0],"is the condition being resolved\n"
    openStep = plan.openconds[0][1]#access the step
    openStep1=openStep
    openCond = plan.openconds[0][0]
    plan.openconds = plan.openconds[1:]
    keep=plan
    if(len(plan.steps)>1):#has init and goal!
    ########################
    #NOT GETTING RID OF THE OPENCOND, SO ASTAR NEVER TAKING IT
    #######################
        if openStep!="init" and openStep!="goal":
            val = openStep.index("*")
            openStep=openStep[:val]
        numPreConds=len(preconds[openStep])
        numStep=len(plan.steps)
        for i in plan.steps:
            i2=i
            plan = keep
            if i!="init" and i!="goal":
                i=i[:i.index("*")]
            if  i !="goal" and i!=openStep:
                for j in adds[i]:
                    bool=0
                    if j==openCond:
                        plan.causallinks.append((i2,openCond,openStep1))#problem
                        plan.ordercons.append((i2,openStep1))
                        PLANCOUNTER+=1
                        plan.id=PLANCOUNTER
                        #threats
                        bol=0
                        for t in plan.steps:#all steps
                            t2=t
                            if t!="init" and t!="goal":
                                val = t.index("*")
                                t=t[:val]
                            for k in deletes[t]:
                                if k == openCond:
                                    for b in plan.ordercons:
                                        if b ==(t,i):
                                            bol=1
                                if bol==0 and t!=i:
                                    for v in plan.threats:
                                        if v[0]==(i2,openCond,openStep1) and v[1]==t2:
                                            bol=1
                                if bol==0 and t!=i and i2!="init":
                                    plan.threats.append(((i2,openCond,openStep1),t2))
                                else:
                                    bol=0
                        ret.append(plan)
                        print len(plan.openconds)+len(plan.threats)," upper\n"
                        plan=keep
    meh=ret
    counter=0
    arr={}
    for k in ret:
        print len(k.openconds)+len(k.threats)," ", k.id,"middle"
        key = counter
        arr[counter]=k
        print arr[counter].id
        counter+=1
    for i in adds:#too many conditions
        stepCons = i
        plan2 = keep
        if i!="goal" and i!="init" and i!=openStep:
            for j in adds[i]:
                if j==openCond:
                    nextStep=i
                    st = str(i)+"*"+str(VARNUM)
                    VARNUM+=1
                    plan2.steps.append(st)
                    plan2.ordercons.append(("init",st))
                    plan2.ordercons.append((st, "goal"))
                    plan2.ordercons.append((st,openStep1))
                    plan2.causallinks.append((st,openCond,openStep1))
                    ##################################################
                    for k in preconds[i]:#issue is htereeeeeeeee
                        plan2.openconds.append((k,st))
                    for k in meh:
                        print len(k.openconds)+len(k.threats)," ", k.id,"middle2s"
                    PLANCOUNTER+=1
                    plan2.id=PLANCOUNTER
                    #threats
                    cnt=0
                    for tr in range(len(arr)):
                        print len(arr[cnt].openconds)+len(arr[cnt].threats)," ", arr[cnt].id,"middlearr"
                        cnt+=1
                    bol=0
                    for t in plan2.steps:#all steps
                        t2=t
                        if t!="init" and t!="goal":
                            val = t.index("*")
                            t=t[:val]
                        for k in deletes[t]:#check their delete list
                            if k == openCond:#if our condition is on our delete lise
                                for b in plan2.ordercons:
                                    if b ==(t,i):# and it is not ordered before it
                                        bol=1
                            if bol==0 and t!=i:
                                for v in plan2.threats:
                                    if v[0]==(i2,openCond,openStep1) and v[1]==t2:
                                        bol=1
                            if bol==0 and t!=i and st!="init":
                                plan2.threats.append(((st,openCond,openStep1),t2))
                            else:
                                bol=0
                            #and y is not before C
                            #causal link, threatening step
                    for k in ret:
                        print len(k.openconds)+len(k.threats)," ", k.id,"middle3"
                    ret.append(plan2)
                    print len(plan2.openconds)+len(plan2.threats)," ",plan2.id," lower\n"
elif len(plan.threats)>0:
    #keep=plan
    openThreatProducer = plan.threats[0][0][0]#access the step
    openThreat=plan.threats[0][1]
    plan.threats=plan.threats[1:]
    print openThreatProducer, " ", openThreat
    i=0
    while i<2:
        plan = keep
        if i==0:
            bool=0
            for k in plan.ordercons:
                if (k[0]==openThreat and k[1]==openThreatProducer) or (k[1]==openThreat and k[0]==openThreatProducer):
                    bool=1
            if bool==0:
                plan.ordercons.append((openThreatProducer,openThreat))
        elif i==1:
            bool=0
            for k in plan.ordercons:
                if (k[0]==openThreat and k[1]==openThreatProducer) or (k[1]==openThreat and k[0]==openThreatProducer):
                    bool=1
            if bool==0:
                plan.ordercons.append((openThreat,openThreatProducer))
        ret.append(plan)
        i+=1
t=len(ret)
for k in ret:
    print len(k.openconds)+len(k.threats)," ", k.id,"lowest"
print t
return ret
4

1 回答 1

0

似乎在做之后plan=argumentplan并且argument指向同一个位置。你应该做这样的事情

import copy  
plan = copy.deepcopy(argument)

这将创建参数的精确副本。

于 2012-11-14T21:31:33.280 回答