Python:如何为字典分配一个具有不同长度的列表的值?

时间:2015-11-18 15:06:49

标签: python for-loop dictionary matrix scipy

我试图在一系列100个2D numpy数组中找到质心,如下所示:

array([[ 0.216,  0.24 ,  0.244, ...,  0.679,  0.684,  0.707],
       [ 0.23 ,  0.229,  0.238, ...,  0.675,  0.676,  0.695],
       [ 0.221,  0.238,  0.24 , ...,  0.669,  0.677,  0.684],
       ..., 
       [ 0.937,  0.925,  0.923, ...,  0.768,  0.754,  0.752],
       [ 0.937,  0.929,  0.923, ...,  0.737,  0.735,  0.741],
       [ 0.934,  0.932,  0.929, ...,  0.72 ,  0.717,  0.728]])

根据像cutoff_values=[0.05,0.1,0.2]这样的一组阈值,我为每个矩阵确定满足cell_value>=0.05cell_value>=0.1cell_value>=0.2的单元格区域。然后,我计算每个区域(或一组细胞)的质心。

这将被执行100次。对于每个阈值,都有一个对应的dictionary,其中我以列表的形式存储质心坐标

对于阈值= 0.05

dict005={'matrix 1':[row for center of mass, column for center of mass]} 对于阈值= 0.1

dict01={...} 对于阈值= 0.2

dict02={...}

但是,由于每个图像可能有多个区域或单元格组cell_value>=0.05(例如),我最终会得到包含两倍于质心的元素的列表(每个质心产生两个)值 - 它的行和列。)

我的问题:我的块中需要更改的内容(见下文)才能正确填充我刚刚定义的dicts基本上,我问的是如何为字典分配一个不同长度列表的值。

示例: 切断= 0.05

dict005={'matrix 1':[17,29],'matrix 2':[23,45,88,101,234,432], 'matrix 3':[0,34,67,86]}

矩阵1有1个质心,值> 0.05,矩阵2有3,矩阵3有2,依此类推,直到达到矩阵100.

我的阻止:

dict005 = OrderedDict()
dict01 = OrderedDict()
dict02 = OrderedDict()

cutoff_values=[0.05, 0.1, 0.2] #A list containing all used cut-off values. 

for i, file in enumerate(os.listdir(directoryPath)):
    if file.endswith(".csv"):

       #The csv_to_matrix is computed here (my matrix of values)
        for val in enumerate(cutoff_values):

            if val == 0.05:
                # Relabels the matrix True or False based on the >= condition, val=0.05 being the first cut-off
                blobs = csv_to_matrix >= val 
                # Creates a few labels and records how many of them there are (nlabels)
                labels, nlabels = ndimage.label(blobs)
                # Empty list to store the coordinates row,column of the center of mass of each region
                list005 = []

                # loop through the nlabels identified
                for i in range(0, nlabels+1):
                    # compute row, column index
                    r, c = numpy.vstack(ndimage.center_of_mass(csv_to_matrix, numpy.arange(nlabels) + 1)).T
                    # add row index as first element of the pair
                    list005[i].append(r)
                    # add column index as second element of the pair
                    list005[i+1].append(c)
                dict005[file] = list005 

            elif val == 0.1:
                # Relabels the matrix True or False based on the >= condition, val=0.1 being the second cut-off
                blobs = csv_to_matrix >= val
                # Creates a few labels and records how many of them there are (nlabels)
                labels, nlabels = ndimage.label(blobs)
                # Empty list to store the coordinates row,column of the center of mass of each region
                list01 = []

                # loop through the nlabels identified
                for i in range(0, nlabels+1):
                    # compute row, column index
                    r, c = numpy.vstack(ndimage.center_of_mass(csv_to_matrix, numpy.arange(nlabels) + 1)).T
                    # add row index as first element of the pair
                    list01[i].append(r)
                    # add column index as second element of the pair
                    list01[i+1].append(c)
                dict01[file] = list01

            elif val == 0.2:
                # Relabels the matrix True or False based on the >= condition, val=0.2 being the third cut-off
                blobs = csv_to_matrix >= val
                # Creates a few labels and records how many of them there are (nlabels)
                labels, nlabels = ndimage.label(blobs)
                # Empty list to store the coordinates row,column of the center of mass of each region
                list02 = []
                # loop through the nlabels identified
                for i in range(0, nlabels+1):
                    # compute row, column index
                    r, c = numpy.vstack(ndimage.center_of_mass(csv_to_matrix, labels, numpy.arange(nlabels) + 1)).T
                    # add row index as first element of the pair
                    list02[i].append(r)
                    # add column index as second element of the pair
                    list02[i+1].append(c)
                dict02[file] = list02

1 个答案:

答案 0 :(得分:1)

不是每次都创建一个新列表,而是检索任何现有列表并附加到它。

一种简单的方法是使用. . .代替*

defaultdict