• python 中实现将三元组数据转换为矩阵形式


    001、

    root@PC1:/home/test3# ls
    a.txt  test.py
    root@PC1:/home/test3# cat test.py              ## 测试程序
    #!/usr/bin/python
    in_file = open("a.txt", "r")
    lines = in_file.readlines()[1:]
    dict1 = dict()
    list1 = list()
    
    for i in lines:
        temp = i.strip().split()
        if temp[0] not in dict1:
            key1 = temp[0]
            dict1[key1] = {}
            key2 = temp[1]
            dict1[key1][key2] = temp[2]
        else:
            key2 = temp[1]
            dict1[key1][key2] = temp[2]
    for i,j in dict1.items():
        for k in j:
            if k not in list1:
                list1.append(k)
    print("gene\\name" + "\t" + "\t".join(list1))
    
    for i,j in dict1.items():
        print(i, end = "\t")
        for k,l in j.items():
            print("\t{}".format(l), end = "")
        print("")
    
    in_file.close()
    root@PC1:/home/test3# cat a.txt                       ## 测试数据
    Gene    Sample  Value
    ENSG00000000460 A-431   25.2
    ENSG00000000460 A-549   14.2
    ENSG00000000460 AN3-CA  10.6
    ENSG00000000460 BEWO    24.4
    ENSG00000000460 CACO-2  14.2
    ENSG00000000938 A-431   0.0
    ENSG00000000938 A-549   0.0
    ENSG00000000938 AN3-CA  0.0
    ENSG00000000938 BEWO    0.0
    ENSG00000000938 CACO-2  0.0
    ENSG00000001084 A-431   19.1
    ENSG00000001084 A-549   155.1
    ENSG00000001084 AN3-CA  24.4
    ENSG00000001084 BEWO    12.6
    ENSG00000001084 CACO-2  23.5
    ENSG00000000457 A-431   2.8
    ENSG00000000457 A-549   3.4
    ENSG00000000457 AN3-CA  3.8
    ENSG00000000457 BEWO    5.8
    ENSG00000000457 CACO-2  2.9
    root@PC1:/home/test3# python test.py | column -t                        ## 程序运行结果
    gene\name        A-431  A-549  AN3-CA  BEWO  CACO-2
    ENSG00000000460  25.2   14.2   10.6    24.4  14.2
    ENSG00000000938  0.0    0.0    0.0     0.0   0.0
    ENSG00000001084  19.1   155.1  24.4    12.6  23.5
    ENSG00000000457  2.8    3.4    3.8     5.8   2.9

    参考:https://www.jianshu.com/p/2475c3240a67

  • 相关阅读:
    Redis Hashes 巧用sort排序
    Redis 压缩存储的配置
    计算
    关于时间大小判断的坑和网上工具类的看法
    Mysql中字段类型之时间戳大坑2
    Mysql中字段类型之时间戳大坑
    Spring和springmvc父子容器注解扫描问题详解
    JXL导出Excel工具类
    Maven学习
    MySQL之账户管理
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16585451.html
Copyright © 2020-2023  润新知