我对我的文件做了一个嵌套字典来分组类中的事件。我想用键值来计算我有多少个类,有多少个最终值。这是我目前掌握的代码:infile = open('ALL','r')
def round_down(num):
return num - (num%100)
count = 0
a = []
split_region = {}
lengths = []
for region in infile:
#print region
(cov,chrm,pos,end,leng) = region.split()
start = int(pos)#-1#-int(leng) ## loosen conditions about break points
end = int(end)
lengths = int(leng)
coverage=int(cov)
rounded_start=round_down(start)
rounded_length=round_down(lengths)
if not (chrm in split_region):
split_region[chrm]={}
if not (rounded_start in split_region[chrm]):
split_region[chrm][rounded_start]={}
if not (rounded_length in split_region[chrm][rounded_start]):
split_region[chrm][rounded_start][rounded_length]= []
split_region[chrm][rounded_start][rounded_length].append({'start':start,'length':lengths,'cov':coverage})
for k,v in split_region[chrm][rounded_start].items():
print len(v),k,v
a.append(len(v))
count +=1
print count
print sum(a)
文件格式如下:
^{pr2}$
所以它基本上是把数字四舍五入到100,然后在我的字典里把它组成一个类。它是嵌套的,因为我首先按取整的起始值分组,然后按取整的长度变量分组。在
在代码的最后,我试图计算有多少个类,我的值的总数是多少。但这会产生不正确的输出:类多于输入文件中的行。怎么解决这个问题?在