为什么这个循环不能正常工作?(Python)

2024-06-06 12:06:33 发布

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

我想做的是:

我正在比较两个文件。在

所以有两个文件包含突变的名称和一个与突变相关联的数字aapos。在

在文件1中,有t个突变数,在文件2中,有s个突变数。在

现在,文件号2中的突变具有特定的生物学意义,所以我要做的是查看文件1中的突变名(在我的程序中称为tagname)是否与文件2中的标记名等效,以及该特定突变的aapos号是否在文件号2中的aapos1和aapos2的范围内。在

如果这两个条件都满足,那么我在文件1中的突变具有特殊的生物学意义,我们称之为1类。否则,就属于第二类,没有生物学意义。在

另外,在每个文件中,都有一个名为synonymous的列,并且对于文件1中的每一个变异,同义列都被分配一个0或1的数字。在

现在我想做的是:对于属于类别2的每个突变,如果该突变的synonymous列为0,我希望计数器nonsyntwo加1,如果它是1,那么我希望计数器syntwo加1。本质上,对于属于类别2的所有突变,我需要一个计数,即在synonymous列中有多少个赋值为0,有多少个赋值为1。在

但是,当程序运行时,我得到了nonsyntwosyntwo的一个数字,这远远大于我正在使用的文件中的标记名数目。如果我将s和{}都改为15(每个文件中都有数千个标记名),那么我得到94作为nonsyntwo的值。在

如果程序迭代15个标记名,这怎么可能?在

for x in range(1,s):
    for b in range (1,t):
        if tagname1[x]== tagname2[b]:   
            if int(aapos1[b]) <= int(aapos[x])<= int(aapos2[b]): 
                snps = snps + 1  
            elif int(synonymous[x]) == 0: 
                nonsyntwo = nonsyntwo + 1
            elif int(synonymous[x]) == 1: 
                syntwo = syntwo + 1 
        elif tagname1[x]!= tagname2[b]: 
            if int(synonymous[x]) == 0: 
                nonsyntwo = nonsyntwo + 1
            elif int(synonymous[x]) == 1: 
                syntwo = syntwo + 1

Tags: 文件标记程序if数字int意义elif
3条回答

你真的需要把你的问题说清楚。我们不应该仅仅为了了解你在处理什么而挑刺你的问题。在

我明白以下几点:

So there are two files that contains names of mutations and a number, called aapos, associated with that mutation. [...] In addition, in each of these files, there is a column called 'synonymous' [...] within the range of aapos1 and aapos2 in file number 2

  1. 你有两个文件。在
  2. 第一个文件有三列:tagname, appos, synonymous。在
  3. 第二个文件有四列:tagname, aapos, appos2, synonymous。在

Now, the mutations in file number 2 have a specific biological signifiance, so what I'm trying to do is see whether the mutation name (called tagname in my program) in file 1 is equivalent to the tagname in file 2, and whether the aapos number for that particular mutation falls within the range of aapos1 and aapos2 in file number 2. [...] If these 2 conditions are met, then the mutation I have in file 1 has a special biological significance, let's call it category 1

  1. 如果第一列与file1中的第一列匹配,则需要检查file2中的每一行
  2. 如果匹配,并且第二列在file2中第二列和第三列的范围内,则需要将该行添加到名为category1的集合中;否则添加到另一个名为category2的集合中。在

in each of these files, there is a column called 'synonymous' and for each mutation in file 1, the synonymous column is assigned a number of 0 or 1. Now this is what I want to do: For each mutation that belongs to CATEGORY 2, if the 'synonymous' column for that mutation is 0, I want the counter 'nonsyntwo' to add 1, and if it is 1, then I want the counter 'syntwo' to add 1.

  1. category2集合中获取每个项目,并计算1和0的数量

如果这是正确的,那么下面就是您需要的(假设您的文件是逗号分隔的)

import csv
from collections import Counter

with open('file1.txt') as f:
   reader = csv.reader(f, delimiter=',')
   file1 = list(reader)

with open('file2.txt') as f:
   reader = csv.reader(f, delimiter=',')
   file2 = list(reader)

cat1 = []
cat2 = []

for line in file2:
   for line2 in file1:
      if line[0] == line2[0]:
          if int(line2[1]) <= int(line[1]) <= int(line2[2]):
              cat1.append(line)
          else:
              cat2.append(line)

counter = Counter(line[3] for line in cat2)
nonsyntwo = counter['0']
syntwo = counter['1']

我不完全理解你想做什么,当我开始命名假设时,它失控了,但这是你的问题:

elif tagname1[x]!= tagname2[b]: 
        if int(synonymous[x]) == 0: 
            nonsyntwo = nonsyntwo + 1
        elif int(synonymous[x]) == 1: 
            syntwo = syntwo + 1

这对每对x,b运行。这意味着它将至少执行次。我怀疑这不是你想要的。在

我不清楚你问题的性质。不过,我有以下改进建议:

aapos = map(int, appos)
aapos1 = map(int, appos1)
aapos2 = map(int, appos2)
synonyms = map(int, synonyms) # or are they already?
for x in range(1,s):
    for b in range (1,t):
        # Here, x and b go from 1 to s resp t.
        # So you are always missing [0] of the array. 
        # Doing
        x, b = x-1, b-1
        # is not very clean, but it helps for now.
        checksyn = True
        if tagname1[x] == tagname2[b]:
            if aapos1[b] <= aapos[x] <= aapos2[b]:
                snps += 1
                checksyn = False
        if checksyn:
            if synonymous[x] == 0: 
                nonsyntwo += 1
            elif synonymous[x] == 1: # if 1 is the only other possible value, an else: would be enough here.
                syntwo += 1

相关问题 更多 >