python 计算字典中键的总数_Python计算字典中的键数

我对我的文件做了一个嵌套字典来分组类中的事件。我想用键值来计算我有多少个类,有多少个最终值。这是我目前掌握的代码: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,然后在我的字典里把它组成一个类。它是嵌套的,因为我首先按取整的起始值分组,然后按取整的长度变量分组。在

在代码的最后,我试图计算有多少个类,我的值的总数是多少。但这会产生不正确的输出:类多于输入文件中的行。怎么解决这个问题?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值