指定要在循环中求和的数字量

2024-06-01 09:03:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我认为用一个例子来解释我的困境是最好的方法。假设我有一个包含以下信息的文件:

isoform    snp_rein
NM_005101    97
NM_005101    144
NM_198576    20790

一本看起来像这样的字典:

exons = {'NM_005101': [(0, 110), (517, 1073)], 'NM_198576': [(0, 251), (2078, 2340), (15154, 15202), (20542, 20758), (21050, 21275), (21355, 21580), (21833, 22040), (23116, 23335), (23415, 23610), (23700, 23901), (23986, 24135), (24211, 24317), (25038, 25155), (25236, 25401), (25610, 25754), (25841, 25966), (26037, 26143), (26274, 26613), (26697, 26835), (27204, 27332), (27450, 27565), (27653, 27773), (27889, 28243), (28744, 28937), (29113, 29329), (29443, 29673), (29780, 29915), (30110, 30207), (30304, 30469), (30603, 30715), (31130, 31247), (31330, 31523), (31605, 31693), (33630, 33855), (34325, 34429), (34701, 35997)]}

我一直在编写一些代码,找出snp_rein数字位于哪对数字之间。然后我可以计算出第一组数字的最大值和第二组数字的最小值之间的差值,依此类推。我的代码如下:

totalintron=0
if name in exons:
    y = exons[name]
    for sd, i  in enumerate(exons[name]):
        if snpos<=max(i):
            exonnumber = sd+1
            position = sd
            print exonnumber
            break
    for index in range(len(y) -1):
            first_max = max(y[index])
            second_min = min(y[index + 1])
            intron = second_min - first_max
            print intron
            totalintron = totalintron + intron
        print totalintron
        totalintron = 0

我的输出看起来是这样的:(**x**表示exonnumber,最后一个数字表示我要更改的总数):

**1**
407
407
**2**
407
407
**5**
1827
12814
5340
292
80
253
1076
80
90
85
76
721
81
209
87
71
131
84
369
118
88
116
501
176
114
107
195
97
134
415
83
82
1937
470
272
28671

我的问题在于总数。我只想合计exonnumber指定的数字量。对于第一个输出,我希望总数为0,因为测试的数字在外显子1的指定范围内。对于第二个输出,我希望总数为407,因为它在外显子2中。对于最后一个输出,我希望将前4个数字相加,因为测试的数字在外显子5中

我希望我的输出是这样的:

**1**
0
**2**
407
**5**
20273

如果有意义的话,有什么建议可以改变我对指定数量的数字求和的方式吗?请解释一下你的建议,因为我是python新手


Tags: nameinindex数字sdminmaxprint
1条回答
网友
1楼 · 发布于 2024-06-01 09:03:24

您希望最后一个内部循环如下所示:

# reset the `totalintron` for the current `exonnumber`
totalintron = 0

# only iterate `exonnumber - 1` (which is guaranteed to be len(y) - 1 at max)
for index in range(exonnumber - 1):
    first_max = max(y[index])
    second_min = min(y[index + 1])
    intron = second_min - first_max
    # don’t print `intron`, we only care about the total
    totalintron = totalintron + intron

print totalintron

相关问题 更多 >