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
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/280477.html