python 2.7 bug还是真的很蠢? python列表

时间:2014-01-27 13:06:40

标签: python python-2.7

我有一个python列表,用于存储直方图。每个直方图本身是一个列表,其中n个条目对应于直方图的区间。每个bin本身又是一个包含三个数字和两个列表的列表:

histograms = [ h1, h2 , h3, ... ],
h1 = [ "name", [ bin_0, bin_1, ..., bin_n ]],
bin_i = [ num_1, num_2, num_2, [ likelihood_sig ], [ likelihood_bkg ] ],
likelihood_x = [ bin_x_0 , ... , bin_x_10 ],

其中bin_i/xnum_i是浮点数。在我的代码中,我扫描数据文件并填充直方图。找到合适的箱子并填充num_i的作品。但是,填充bin_x不会。无论我写入likelihood_sig的是什么,都会自动(对我来说感觉像魔术:P)写入likelihood_bkg,反之亦然。

这是代码的一部分,我将数据填充到列表结构中(当我试图弄清楚什么是错误时,有很多打印语句......):

# 1d histograms
for histogram in self._histograms_1d:
    # definition is a string with the name of the histogram
    definition = histogram[0].strip().split()

    # particles is a list of a list with four floats
    # particles = [ p1, p2, ... ] , p1 = [ x0, x1, x2, x3 ]

    actual_bin = self.which_bin_1d(particles,definition)
    # add the data (dsig_s,dsig_b) to the correct bin (works)
    histogram[1][actual_bin][1]         = histogram[1][actual_bin][1]         + dsig_s
    histogram[1][actual_bin][2]         = histogram[1][actual_bin][2]         + dsig_b
    # dq_bin = int ( log ( 1 + dsig_s/dsig_b) // binning_size (=0.5) ) ... works
    # add data to likelihood (doesn't work ... )
    my_debug_temp2 =  histogram[1][actual_bin][4][dq_bin]
    my_debug_temp1 =  histogram[1][actual_bin][3][dq_bin] + dsig_s
    histogram[1][actual_bin][3][dq_bin] = my_debug_temp1
    histogram[1][actual_bin][4][dq_bin] = my_debug_temp2
    my_debug_temp1 =  histogram[1][actual_bin][3][dq_bin]
    my_debug_temp2 =  histogram[1][actual_bin][4][dq_bin] + dsig_b
    histogram[1][actual_bin][4][dq_bin] = my_debug_temp2
    histogram[1][actual_bin][3][dq_bin] = my_debug_temp1
    # in the following print statement I find histogram[1][actual_bin][4][dq_bin] = histogram[1][actual_bin][4][dq_bin] (??? independent of my_debug_temp2 and my_debug_temp1 !!)
    print histogram[1][actual_bin][3]
    print histogram[1][actual_bin][4]

如果有人知道如何处理这个问题,或者告诉我是否错误地使用了某些Python语句,那会很好。

编辑:减少打印的

编辑:直方图的初始阶段

这是我定义直方图结构的代码部分。然后在上面的部分填补。它们不会在其他任何地方使用。

#================================================================
# load cuts from cut file
def load_1d(self):
    for line in open(self._histogram_1d_file):
        if line.strip()[:1]!='#' and len(line.strip())>0:
            #print line.strip()
            words = line.strip().split()
            nbins = int(words[1])
            histogram = []
            dq_vec = []
            for i in range(0,self._num_dq_bins):
                dq_vec.append(0.0)
            for i in range(0,nbins):
                bin_i=[0,0,0,dq_vec,dq_vec]
                histogram.append(bin_i)
            self._histograms_1d.append([line.strip(),histogram])
    return True
#================================================================

1 个答案:

答案 0 :(得分:1)

我打赌你的likelihood_siglikelihood_bkg指向不同名称下的同一个列表对象。您需要修复初始化代码以为后者实例化另一个列表对象。

a = [1,2,3]
b = a
b[1] = 20
a[1] #=> 20 because a and b point to the same list object

a = [1,2,3]
b = a[:]
b[1] = 20
a[1] #=> 2

此处a[:]a[0:len(a)]的简写,它会生成a的(浅)副本。