Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字典字典创建带有占位符的制表符分隔矩阵_Python_Dictionary_Matrix - Fatal编程技术网

Python 从字典字典创建带有占位符的制表符分隔矩阵

Python 从字典字典创建带有占位符的制表符分隔矩阵,python,dictionary,matrix,Python,Dictionary,Matrix,我有一套500个细胞和另外一套大约12个基因。我还有一本字典,里面有细胞到基因的映射,还有基因到计数的映射。我想把这些信息组织成一个矩阵,我们把细胞作为列名,把基因作为行名。每一个基因细胞都会包含计数。如果该特定单元格没有计数,则用零占位符填充 这里是一个玩具的例子。假设您获得了以下数据: cells = set(['cell_1', 'cell_2']) genes = set(['gene_a', 'gene_b', 'gene_c', 'gene_d', 'gene_e', 'gene_f

我有一套500个细胞和另外一套大约12个基因。我还有一本字典,里面有细胞到基因的映射,还有基因到计数的映射。我想把这些信息组织成一个矩阵,我们把细胞作为列名,把基因作为行名。每一个基因细胞都会包含计数。如果该特定单元格没有计数,则用零占位符填充

这里是一个玩具的例子。假设您获得了以下数据:

cells = set(['cell_1', 'cell_2'])
genes = set(['gene_a', 'gene_b', 'gene_c', 'gene_d', 'gene_e', 'gene_f'])
test_data = {'cell_2': {'gene_c': 13, 'gene_f': 6}, 
             'cell_1': {'gene_a': 12, 'gene_c': 2}}
我们希望创建一个选项卡分隔的表,如下所示:

            cell_1| cell_2
    -------|------|-------
    gene_a | 12   | 0
    gene_b | 0    | 0
    gene_c | 2    | 13
    gene_d | 0    | 0
    gene_e | 0    | 0
    gene_f | 0    | 6

这里的最终目标是以制表符分隔的格式写出这个矩阵。任何帮助都将不胜感激

以下是一个输出选项卡分隔文件的解决方案:

cells = ['cell_1', 'cell_2']
genes = ['gene_a', 'gene_b', 'gene_c', 'gene_d', 'gene_e', 'gene_f']
test_data = {'cell_2': {'gene_c': 13, 'gene_f': 6}, 
             'cell_1': {'gene_a': 12, 'gene_c': 2}}
with open("genes.csv", "w") as f:
    f.write("name")
    for cell in cells:
        f.write("\t")
        f.write(cell)
    for gene in genes:
        f.write("\n")
        f.write(gene)
        for cell in cells:
            f.write("\t")
            if cell in test_data and gene in test_data[cell]:
                val = test_data[cell][gene]
            else:
                val = 0 # this could be considered an error
            f.write(str(val))
该文件如下所示:

name    cell_1  cell_2
gene_a  12  0
gene_b  0   0
gene_c  2   13
gene_d  0   0
gene_e  0   0
gene_f  0   6
我把细胞和基因作为列表而不是集合,以维持它们的顺序。列表或集合都没有效率差异,所以如果您已经将它们设置为集合,也可以


我为任何没有数据的细胞/基因输出零,您可能更愿意将其更改为空白或引发错误。

python疗法会使用列表理解。此外,将流程分为几个小步骤使其易于阅读和维护

def make_matrix(genes, cells, data):
    return [[data[cell].get(gene, 0) for cell in sorted(cells)] for gene in sorted(genes)]

def add_headers(row_headers, column_headers, matrix):
    annotated_matrix = [[header] + row for header, row in zip(sorted(row_headers), matrix)]
    return [[""] + list(column_headers)] + annotated_matrix

def format_matrix(matrix):
    return '\n'.join(['\t'.join([str(item) for item in row]) for row in matrix])

print(format_matrix(add_headers(genes, cells, make_matrix(genes, cells, test_data))))