ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-08-14 15:00:40  阅读:153  来源: 互联网

标签:ENSG00000000938 dict1 temp python 0.0 ENSG00000000457 矩阵 三元组 ENSG00000000460


 

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有