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

2022/8/15 1:53:56

本文主要是介绍python 中实现将三元组数据转换为矩阵形式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

001、

[email protected]:/home/test3# ls
a.txt  test.py
[email protected]:/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()
[email protected]:/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
[email protected]:/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

 



这篇关于python 中实现将三元组数据转换为矩阵形式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程