用于比较2个文件的嵌套循环

时间:2011-10-20 01:58:09

标签: python loops for-loop

我正在编写一个比较两个文件的程序。对于文件1中的每一行,我想将它与文件2中的所有行进行比较,然后继续文件1中的下一行。在第一次命中后,程序不会在文件1中继续。有什么建议吗?

代码:全选

#! /usr/bin/env python

import sys
import fileinput

# Open the two files
f1 = open(sys.argv[1], "r")
f2 = open(sys.argv[2], "r")

for line in f1:
    chrR,chrStart,chrEnd,name,score,strand1,codingStart,codingEnd,itemRbg,blockCount,blockSize,BlockStart = line.strip().split()
    chr = range(int(chrStart), int(chrEnd))
    lncRNA = set(chr)
    for line in f2:
        chrC,clustStart,clustEnd,annote,score,strand = line.strip().split()
        clust = range(int(clustStart), int(clustEnd))
        cluster = set(clust)
        if strand1 == '-':
            if chrR == chrC:
                if strand1 == strand:
                    if cluster & lncRNA:
                         print name,annote,'transcript'
                         continue
                     else:
                         continue
                 continue
        break

3 个答案:

答案 0 :(得分:3)

f1中的第一行之后,您已经读取了f2文件中的所有行,因此for line2 in f2f1文件中的第二行和后续行没有迭代,除非f2文件在磁盘上增长。

#!/usr/bin/env python
import sys

def intersect(r1, r2):
    return r2[0] < (r1[-1]+1) and r1[0] < (r2[-1]+1)

with open(sys.argv[2]) as f2:
     chrC_set, strand_set, clusters = set(), set(), []
     for i, line in enumerate(f2):
         parts = line.split()
         if len(parts) != 6:
            print >>sys.stderr, "%d line has %d parts: %s" % (i, len(parts), line),
            continue
         chrC, clustStart, clustEnd, annote, _, strand = parts
         chrC_set.add(chrC)
         strand_set.add(strand)
         clusters.append((xrange(int(clustStart), int(clustEnd)), annote))

with open(sys.argv[1]) as f1:
     for i, line in enumerate(f1):
         parts = line.split()
         if len(parts) < 6:
            print >>sys.stderr, "%d line has %d parts: %s" % (i, len(parts), line),
            continue
         chrR, chrStart, chrEnd, name, _, strand1 = parts[:6]
         if strand1 == '-' and chrR in chrC_set and strand1 in strand_set:
            lncRNA = xrange(int(chrStart), int(chrEnd))
            for cluster, annote in clusters:
                if intersect(cluster, lncRNA):
                   print name, annote, 'transcript'

答案 1 :(得分:1)

测试if strand1 == '-'不依赖于 f2 的内容。因此,只有当 f1 的当前行包含一个 f2 时,才可以在 f2 循环之前将其置于 f2 的所有内容的检查中 strand1 ,其值为' - '

还有第一个if strand1 == '-'然后if strand1 == strand的事实,这意味着您也只对 f2 链的行感兴趣的值为“ - ”。

此外,我采用了J.F.Sebastian的想法,在没有集合帮助的情况下测试两个范围的交集,但仅测试范围的界限。但是,没有必要使用范围 xrange ,测试边界就足够了。

所以,我提出以下代码,作为算法的简单改进:

for line in f1:
    (chrR,chrStart,chrEnd,name,score,strand1,codingStart,codingEnd,
     itemRbg,blockCount,blockSize,BlockStart) = line.strip().split()
    if strand1 == '-':
        s,e = int(chrStart), int(chrEnd)
        for line in f2:
            chrC,clustStart,clustEnd,annote,score,strand = line.strip().split()
            if strand=='-' and chrR == chrC \
               and int(clustStart)<e and s<int(clustEnd):
                print name,annote,'transcript'
        f2.seek(0,0)

然而,这个算法(你的,纠正的)很差:对于包含 strand1的 f1 的每一行,有一个完整的 f2 内容读数值为' - '。

J.F.Sebastian的算法要好得多 我用上面提到的想法对它进行了一点改进。

with open(sys.argv[2]) as f2:
    clusters = []
    for i, line in enumerate(f2):
        parts = line.split()
        if len(parts) != 6:
            print >>sys.stderr, "%d line has %d parts: %s" % (i,len(parts),line),
            continue
        chrC, clustStart, clustEnd, annote, _, strand = parts
        if strand=='-':
            clusters.append((chrC, int(clustStart), int(clustEnd), annote))

with open(sys.argv[1]) as f1:
    for i, line in enumerate(f1):
        parts = line.split()
        if len(parts) < 6:
            print >>sys.stderr, "%d line has %d parts: %s" % (i,len(parts),line),
            continue
        chrR, chrStart, chrEnd, name, _, strand1 = parts[:6]
        if strand1 == '-':
            for chrC,iclustStart,iclustEnd,annote in clusters:
                if chrR == chrC \
                   and iclustStart<int(chrEnd) and int(chrStart)<iclustEnd:
                    print name, annote, 'transcript'

答案 2 :(得分:0)

在找到第一个点击后,你故意做“继续”。然后在第一行之后也做“休息”。

你不需要这样做。第二个循环将继续到f2中的下一行就好了。 然后,当它到达f2的末尾 - 它将进入f1的下一行。 如果你想要检查f1中每条线对f2中的每一行,那么所有那些继续(并且中断)都是多余的。

尝试:

for line in f1:
     chrR,chrStart,chrEnd,name,score,strand1,codingStart,codingEnd,itemRbg,blockCount,blockSize,BlockStart = line.strip().split()
    chr = range(int(chrStart), int(chrEnd))
    lncRNA = set(chr)
    for line2 in f2:
            chrC,clustStart,clustEnd,annote,score,strand = line2.strip().split()
            clust = range(int(clustStart), int(clustEnd))
            cluster = set(clust)
            if strand1 == '-':
                    if chrR == chrC:
                            if strand1 == strand:
                                    if cluster & lncRNA:
                                            print name,annote,'transcript'
相关问题