Python:如何为字典分配一个长度可变的列表值?

Python:如何为字典分配一个长度可变的列表值?,python,for-loop,dictionary,matrix,scipy,Python,For Loop,Dictionary,Matrix,Scipy,我试图在一系列100个2Dnumpy阵列中定位质心,如下所示: 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.

我试图在一系列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]])
基于一组阈值,如
截止值=[0.05,0.1,0.2]
,我为每个矩阵确定满足
单元值>=0.05
单元值>=0.1
单元值>=0.2
的单元区域。然后,我计算每个区域(或细胞组)的质心

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

dict005={'matrix 1':[row代表质心,column代表质心]}
for threshold=0.05

dict01={…}
for threshold=0.1

dict02={…}
for threshold=0.2

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

我的问题:为了正确填写我刚刚定义的
指令,我的代码块(见下文)中需要更改哪些内容基本上,我想问的是如何为字典分配一个长度可变的列表值。

示例: 截止值=0.05

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

对于大于0.05的值,矩阵1有1个质心,矩阵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

与其每次都创建一个新列表,不如检索任何现有列表并附加到其中

一个简单的方法是使用
defaultdict
而不是
orderedict

from collections import defaultdict
d = defaultdict(list)
d['somefile.csv'].extend([3, 4])
print(d)
# defaultdict(<type 'list'>, {'somefile.csv': [3, 4]})
d['somefile.csv']
# [3, 4]
从集合导入defaultdict
d=默认DICT(列表)
d['somefile.csv'].extend([3,4])
印刷品(d)
#defaultdict(,{'somefile.csv':[3,4]})
d['somefile.csv']
# [3, 4]

您可以将字典项的值设置为几乎任何值,并且可以随意分配它们。但是,检查一下
OrderedDict
的定义,看看重新分配是如何影响订单的(如果有的话)。好吧,我不知道我的代码中有任何类型的重新分配。for块意味着在我的100个矩阵中循环,每个新矩阵都会得到一个新的键
矩阵n
和一个新的列表作为值。因此,我无法理解您为什么要指出顺序……附带说明:您可以始终以交互方式调用Python,并查看什么工作以及如何工作。例如,
打印{'a':[1,2],'b':[3,4,5,6]}
。是的,谢谢,我用这个来做一些调试。这意味着所有的时间。。。