你可以看到文件中的那樣:嵌套的字典和值
LOC_Os06g07630.1 cyto 8, chlo 2, extr 2, nucl 1, cysk 1, chlo_mito 1, cysk_nucl 1
LOC_Os06g12160.1 chlo 7, nucl 3, mito 2.5, cyto_mito 2
LOC_Os06g39870.1 chlo 7, cyto 4, nucl 1, E.R. 1, pero 1
LOC_Os06g48240.1 chlo 9, mito 4
LOC_Os06g48250.1 cyto 5, chlo 4, mito 2, pero 2
我在乎「氯仿溶劑」和「chlo_mito」和「美圖」,而和值每一行中
像行LOC_Os06g07630.1,我將使用氯仿溶劑2和chlo_mito 1, 總和值是3 =(氯仿溶劑)2+(chlo_mito)1個
所述行總和值是
(細胞學)8+(氯仿溶劑)2+(抽)2+(NUCL)1+(CY SK)1+(chlo_mito)1+(cysk_nucl)1 = 16,然後打印3/16
我想下一個內容:
LOC_Os06g07630.1 chlo 2 chlo_mito 1 3/16
LOC_Os06g12160.1 chlo 7 mito 2.5 9.5/14.5
LOC_Os06g39870.1 chlo 7 7/15
LOC_Os06g48240.1 chlo 9 mito 4 13/13
LOC_Os06g48250.1 chlo 4 mito 2 6/13
我的代碼是:
import re
dic={}
b=re.compile("chlo|mito|chlo_mito")
with open("~/A","r") as f1:
for i in f1:
if i.startswith("#"):continue
a=i.replace(',',"").replace(" ","/")
m=b.search(a)
if m is not None:
dic[a.strip().split("/")[0]]={}
temp=a.strip().split("/")[1:]
c=range(1,len(temp),2)
for x in c:
dic[a.strip().split("/")[0]][temp[x-1]]=temp[x]
#print dic
lis=["chlo","mito","chlo_mito"]
for k in dic:
sum_value=0
sum_values=0
for x in dic[k]:
sum_value=sum_value+float(dic[k][x])
for i in lis:
#sum_values=0
if i in dic[k]:
#print i,dic[k][i]
sum_values=sum_value+float(dic[k][i])
print k,dic[k],i,sum_values
#print k,dic[k]
但是每一行都有其他像「nucl」等等,它們的數目是不同的 – zychen