如何理解python中的变量范围?

时间:2015-05-09 04:55:49

标签: python

def gibsSample(pB,pC,pA_B,pD_BC,pE_D,pF_C,label_A,label_B,label_C,label_D,label_E,label_F,T):
    #initial state
    sample=['A=F','B=n','C=F','D=h','E=F','F=F']
    sampleState=[]
    for i in range(T):
        #draw sample A from p(A | B,C,D,E,F)
        sample_A=sampleA(sample,pA_B)
        sample[0]=sample_A
        #draw sample B from p(B | A,C,D,E,F)
        sample_B=sampleB(sample,pB,pA_B,pD_BC)
        sample[1]=sample_B
        #draw sample C from p(C | A,B,D,E,F)
        sample_C=sampleC(sample,pC,pD_BC,pF_C)
        sample[2]=sample_C
        #draw sample D from p(D | A,B,C,E,F)
        sample_D=sampleD(sample,pD_BC,pE_D)
        sample[3]=sample_D
        #draw sample E from p(E | A,B,C,D,F)
        sample_E=sampleE(sample,pE_D)
        sample[4]=sample_E
        #draw sample F from p(F | A,B,C,D,E)
        sample_F=sampleF(sample,pF_C)
        sample[5]=sample_F
        sampleState.append(sample)
    return sampleState

我写了一个示例算法。每次从一个分布中采样后,变量'sample'中的元素将被修改,并且在一个圆之后,我想将它附加到列表'sampleState'中。但为什么看起来'sampleState'只附加了初始'样本'而不是我修改过的样本?我迭代循环2000次,'sampleState'中的所有元素与初始状态相同,但我在每次迭代中检查'sample'没有问题。

2 个答案:

答案 0 :(得分:1)

每当您将sample附加到列表中时,您都会附加对完全相同对象的引用。

在每个循环之后,您将更改此对象内的值(sample列表的每个索引处的值),因此如果列表中的所有对象都指向同一个对象,则它们都将看到相同的值

要解决此问题,请在每个循环结束时创建一个新列表:

    def gibsSample(pB,pC,pA_B,pD_BC,pE_D,pF_C,label_A,label_B,label_C,label_D,label_E,label_F,T):
        #initial state
        sample=['A=F','B=n','C=F','D=h','E=F','F=F']
        sampleState=[]
        for i in range(T):
            #draw sample A from p(A | B,C,D,E,F)
            sample_A=sampleA(sample,pA_B)
            sample[0]=sample_A
            #draw sample B from p(B | A,C,D,E,F)
            sample_B=sampleB(sample,pB,pA_B,pD_BC)
            sample[1]=sample_B
            #draw sample C from p(C | A,B,D,E,F)
            sample_C=sampleC(sample,pC,pD_BC,pF_C)
            sample[2]=sample_C
            #draw sample D from p(D | A,B,C,E,F)
            sample_D=sampleD(sample,pD_BC,pE_D)
            sample[3]=sample_D
            #draw sample E from p(E | A,B,C,D,F)
            sample_E=sampleE(sample,pE_D)
            sample[4]=sample_E
            #draw sample F from p(F | A,B,C,D,E)
            sample_F=sampleF(sample,pF_C)
            sample[5]=sample_F
            sampleState.append(list(sample)) # here you create a copy of your "sample"
        return sampleState

替代解决方案

因为您使用的是固定大小的列表,所以您可能还想使用6元组:

def gibsSample(pB,pC,pA_B,pD_BC,pE_D,pF_C,label_A,label_B,label_C,label_D,label_E,label_F,T):
    #initial state
    sample=('A=F','B=n','C=F','D=h','E=F','F=F')
    sampleState=[]
    for i in range(T):
        #draw sample A from p(A | B,C,D,E,F)
        sample_A=sampleA(sample,pA_B)

        #draw sample B from p(B | A,C,D,E,F)
        sample_B=sampleB(sample,pB,pA_B,pD_BC)

        #draw sample C from p(C | A,B,D,E,F)
        sample_C=sampleC(sample,pC,pD_BC,pF_C)

        #draw sample D from p(D | A,B,C,E,F)
        sample_D=sampleD(sample,pD_BC,pE_D)

        #draw sample E from p(E | A,B,C,D,F)
        sample_E=sampleE(sample,pE_D)

        #draw sample F from p(F | A,B,C,D,E)
        sample_F=sampleF(sample,pF_C)

        t = (sample_A, sample_B, sample_C, sample_D, sample_E, sample_F)
        sampleState.append(t)
    return sampleState

答案 1 :(得分:0)

您正在寻找的是函数中的静态变量。正如this问题中所述,您需要在代码中的任何位置替换sample gibsSample.sample

def gibsSample(pB,pC,pA_B,pD_BC,pE_D,pF_C,label_A,label_B,label_C,label_D,label_E,label_F,T):
    #initial state
    gibsSample.sample=['A=F','B=n','C=F','D=h','E=F','F=F']
    sampleState=[]
    for i in range(T):
        #draw sample A from p(A | B,C,D,E,F)
        sample_A=sampleA(gibsSample.sample,pA_B)
        gibsSample.sample[0]=sample_A
        #draw sample B from p(B | A,C,D,E,F)
        sample_B=sampleB(gibsSample.sample,pB,pA_B,pD_BC)
        gibsSample.sample[1]=sample_B
        #draw sample C from p(C | A,B,D,E,F)
        sample_C=sampleC(gibsSample.sample,pC,pD_BC,pF_C)
        gibsSample.sample[2]=sample_C
        #draw sample D from p(D | A,B,C,E,F)
        sample_D=sampleD(gibsSample.sample,pD_BC,pE_D)
        gibsSample.sample[3]=sample_D
        #draw sample E from p(E | A,B,C,D,F)
        sample_E=sampleE(gibsSample.sample,pE_D)
        gibsSample.sample[4]=sample_E
        #draw sample F from p(F | A,B,C,D,E)
        sample_F=sampleF(gibsSample.sample,pF_C)
        gibsSample.sample[5]=sample_F
        sampleState.append(gibsSample.sample)
        gibsSample.sample = [None] * 6
    return sampleState