编程语言
首页 > 编程语言> > python 中实现将三元组数据转换为矩阵形式

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

 

标签:ENSG00000000938,dict1,temp,python,0.0,ENSG00000000457,矩阵,三元组,ENSG00000000460
来源: https://www.cnblogs.com/liujiaxin2018/p/16585451.html